This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorConversion.hpp
58 lines (49 loc) · 2.16 KB
/
ColorConversion.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include "framework.h"
#include "cpprt.h"
#include "winrt.h"
namespace AcrylicEverywhere
{
// Helper classes used for converting between RGB, HSV, and hex.
class Rgb
{
public:
double r{};
double g{};
double b{};
Rgb() = default;
Rgb(double r, double g, double b);
};
class Hsv
{
public:
double h{};
double s{};
double v{};
Hsv() = default;
Hsv(double h, double s, double v);
};
std::optional<unsigned long> TryParseInt(const std::wstring_view& s);
std::optional<unsigned long> TryParseInt(const std::wstring_view& str, int base);
Hsv RgbToHsv(const Rgb& rgb);
Rgb HsvToRgb(const Hsv& hsv);
Rgb HexToRgb(const std::wstring_view& input);
winrt::hstring RgbToHex(const Rgb& rgb);
std::tuple<Rgb, double> HexToRgba(const std::wstring_view& input);
winrt::hstring RgbaToHex(const Rgb& rgb, double alpha);
winrt::Windows::UI::Color ColorFromRgba(const Rgb& rgb, double alpha = 1.0);
Rgb RgbFromColor(const winrt::Windows::UI::Color& color);
// We represent HSV and alpha using a Vector4 (float4 in C++/WinRT).
// We'll use the following helper methods to convert between the four dimensions and HSVA.
namespace hsv
{
inline float GetHue(const winrt::Windows::Foundation::Numerics::float4& hsva) { return hsva.x; }
inline void SetHue(winrt::Windows::Foundation::Numerics::float4& hsva, float hue) { hsva.x = hue; }
inline float GetSaturation(const winrt::Windows::Foundation::Numerics::float4& hsva) { return hsva.y; }
inline void SetSaturation(winrt::Windows::Foundation::Numerics::float4& hsva, float saturation) { hsva.y = saturation; }
inline float GetValue(const winrt::Windows::Foundation::Numerics::float4& hsva) { return hsva.z; }
inline void SetValue(winrt::Windows::Foundation::Numerics::float4& hsva, float value) { hsva.z = value; }
inline float GetAlpha(const winrt::Windows::Foundation::Numerics::float4& hsva) { return hsva.w; }
inline void SetAlpha(winrt::Windows::Foundation::Numerics::float4& hsva, float alpha) { hsva.w = alpha; }
}
}