mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
[FancyZones] HEX to RGB util (#6275)
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
VersionHelper::VersionHelper(std::string str)
|
||||
{
|
||||
// Remove whitespaces chars and a leading 'v'
|
||||
str = left_trim(trim(str), "v");
|
||||
str = left_trim<char>(trim<char>(str), "v");
|
||||
// Replace '.' with spaces
|
||||
replace_chars(str, ".", ' ');
|
||||
|
||||
|
||||
@@ -4,21 +4,28 @@
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
constexpr inline std::string_view default_trim_arg = " \t\r\n";
|
||||
|
||||
inline std::string_view left_trim(std::string_view s, const std::string_view chars_to_trim = default_trim_arg)
|
||||
template<typename CharT>
|
||||
inline constexpr std::basic_string_view<CharT> default_trim_arg()
|
||||
{
|
||||
s.remove_prefix(std::min(s.find_first_not_of(chars_to_trim), size(s)));
|
||||
return reinterpret_cast<CharT*>(" \t\r\n");
|
||||
}
|
||||
|
||||
template <typename CharT>
|
||||
inline std::basic_string_view<CharT> left_trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>())
|
||||
{
|
||||
s.remove_prefix(std::min<size_t>(s.find_first_not_of(chars_to_trim), size(s)));
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string_view right_trim(std::string_view s, const std::string_view chars_to_trim = default_trim_arg)
|
||||
template<typename CharT>
|
||||
inline std::basic_string_view<CharT> right_trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>())
|
||||
{
|
||||
s.remove_suffix(std::min(size(s) - s.find_last_not_of(chars_to_trim) - 1, size(s)));
|
||||
s.remove_suffix(std::min<size_t>(size(s) - s.find_last_not_of(chars_to_trim) - 1, size(s)));
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string_view trim(std::string_view s, const std::string_view chars_to_trim = default_trim_arg)
|
||||
template<typename CharT>
|
||||
inline std::basic_string_view<CharT> trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>())
|
||||
{
|
||||
return left_trim(right_trim(s, chars_to_trim), chars_to_trim);
|
||||
}
|
||||
|
||||
@@ -135,35 +135,17 @@ public:
|
||||
IFACEMETHODIMP_(COLORREF)
|
||||
GetZoneColor() noexcept
|
||||
{
|
||||
// Skip the leading # and convert to long
|
||||
const auto color = m_settings->GetSettings()->zoneColor;
|
||||
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
||||
const auto nR = (tmp & 0xFF0000) >> 16;
|
||||
const auto nG = (tmp & 0xFF00) >> 8;
|
||||
const auto nB = (tmp & 0xFF);
|
||||
return RGB(nR, nG, nB);
|
||||
return (FancyZonesUtils::HexToRGB(m_settings->GetSettings()->zoneColor));
|
||||
}
|
||||
IFACEMETHODIMP_(COLORREF)
|
||||
GetZoneBorderColor() noexcept
|
||||
{
|
||||
// Skip the leading # and convert to long
|
||||
const auto color = m_settings->GetSettings()->zoneBorderColor;
|
||||
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
||||
const auto nR = (tmp & 0xFF0000) >> 16;
|
||||
const auto nG = (tmp & 0xFF00) >> 8;
|
||||
const auto nB = (tmp & 0xFF);
|
||||
return RGB(nR, nG, nB);
|
||||
return (FancyZonesUtils::HexToRGB(m_settings->GetSettings()->zoneBorderColor));
|
||||
}
|
||||
IFACEMETHODIMP_(COLORREF)
|
||||
GetZoneHighlightColor() noexcept
|
||||
{
|
||||
// Skip the leading # and convert to long
|
||||
const auto color = m_settings->GetSettings()->zoneHighlightColor;
|
||||
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
||||
const auto nR = (tmp & 0xFF0000) >> 16;
|
||||
const auto nG = (tmp & 0xFF00) >> 8;
|
||||
const auto nB = (tmp & 0xFF);
|
||||
return RGB(nR, nG, nB);
|
||||
return (FancyZonesUtils::HexToRGB(m_settings->GetSettings()->zoneHighlightColor));
|
||||
}
|
||||
IFACEMETHODIMP_(int)
|
||||
GetZoneHighlightOpacity() noexcept
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "gdiplus.h"
|
||||
#include <common/string_utils.h>
|
||||
|
||||
namespace FancyZonesUtils
|
||||
{
|
||||
@@ -96,6 +97,24 @@ namespace FancyZonesUtils
|
||||
SRCCOPY);
|
||||
}
|
||||
|
||||
inline COLORREF HexToRGB(std::wstring_view hex, const COLORREF fallbackColor = RGB(255, 255, 255))
|
||||
{
|
||||
hex = left_trim<wchar_t>(trim<wchar_t>(hex), L"#");
|
||||
|
||||
try
|
||||
{
|
||||
const long long tmp = std::stoll(hex.data(), nullptr, 16);
|
||||
const BYTE nR = (tmp & 0xFF0000) >> 16;
|
||||
const BYTE nG = (tmp & 0xFF00) >> 8;
|
||||
const BYTE nB = (tmp & 0xFF);
|
||||
return RGB(nR, nG, nB);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
return fallbackColor;
|
||||
}
|
||||
}
|
||||
|
||||
inline void ParseDeviceId(PCWSTR deviceId, PWSTR parsedId, size_t size)
|
||||
{
|
||||
// We're interested in the unique part between the first and last #'s
|
||||
|
||||
@@ -232,6 +232,27 @@ namespace FancyZonesUnitTests
|
||||
CustomAssert::AreEqual(firstTime, monitorInfoCopy);
|
||||
} while (next_permutation(monitorInfoPermutation.begin(), monitorInfoPermutation.end(), [](auto x, auto y) { return x.first < y.first; }));
|
||||
}
|
||||
|
||||
TEST_METHOD(TestHexToRGB_rgb)
|
||||
{
|
||||
const auto expected = RGB(163, 246, 255);
|
||||
const auto actual = HexToRGB(L"#A3F6FF");
|
||||
Assert::AreEqual(expected, actual);
|
||||
}
|
||||
|
||||
TEST_METHOD (TestHexToRGB_argb)
|
||||
{
|
||||
const auto expected = RGB(163, 246, 255);
|
||||
const auto actual = HexToRGB(L"#FFA3F6FF");
|
||||
Assert::AreEqual(expected, actual);
|
||||
}
|
||||
|
||||
TEST_METHOD (TestHexToRGB_invalid)
|
||||
{
|
||||
const auto expected = RGB(255, 255, 255);
|
||||
const auto actual = HexToRGB(L"zzz");
|
||||
Assert::AreEqual(expected, actual);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user