forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlend.cs
More file actions
75 lines (71 loc) · 3.16 KB
/
Blend.cs
File metadata and controls
75 lines (71 loc) · 3.16 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using static System.Math;
using static MaterialColorUtilities.MathUtils;
namespace MaterialColorUtilities;
/// <summary>
/// Functions for blending in HCT and CAM16.
/// </summary>
public static class Blend
{
/// <summary>
/// Blend the design color's HCT hue towards the key color's HCT hue, in a way that
/// leaves the original color recognizable and recognizably shifted towards the key color.
/// </summary>
/// <param name="designColor">ARGB representation of an arbitrary color.</param>
/// <param name="sourceColor">ARGB representation of the main theme color.</param>
/// <returns>
/// The design color with a hue shifted towards the system's color, a slightly
/// warmer/cooler variant of the design color's hue.
/// </returns>
public static int Harmonize(int designColor, int sourceColor)
{
var fromHct = Hct.FromInt(designColor);
var toHct = Hct.FromInt(sourceColor);
double differenceDegrees = DifferenceDegrees(fromHct.Hue, toHct.Hue);
double rotationDegrees = Min(differenceDegrees * 0.5, 15.0);
double outputHue = SanitizeDegreesDouble(
fromHct.Hue + rotationDegrees * RotationDirection(fromHct.Hue, toHct.Hue));
return Hct.From(outputHue, fromHct.Chroma, fromHct.Tone).Argb;
}
/// <summary>
/// Blends hue from one color into another. The chroma and tone of the original color are maintained.
/// </summary>
/// <param name="from">ARGB representation of color.</param>
/// <param name="to">ARGB representation of color.</param>
/// <param name="amount">How much blending to perform; 0.0 >= and <= 1.0.</param>
/// <returns>
/// <paramref name="from"/> with a hue blended towards <paramref name="to"/>.
/// Chroma and tone are constant.
/// </returns>
public static int HctHue(int from, int to, double amount)
{
int ucs = Cam16Ucs(from, to, amount);
var ucsCam = Cam16.FromInt(ucs);
var fromCam = Cam16.FromInt(from);
var blended = Hct.From(ucsCam.GetHue(), fromCam.GetChroma(), ColorUtils.LstarFromArgb(from));
return blended.Argb;
}
/// <summary>
/// Blend in CAM16-UCS space.
/// </summary>
/// <param name="from">ARGB representation of color.</param>
/// <param name="to">ARGB representation of color.</param>
/// <param name="amount">How much blending to perform; 0.0 >= and <= 1.0.</param>
/// <returns>
/// <paramref name="from"/>, blended towards <paramref name="to"/>. Hue, chroma, and tone will change.
/// </returns>
public static int Cam16Ucs(int from, int to, double amount)
{
var fromCam = Cam16.FromInt(from);
var toCam = Cam16.FromInt(to);
double fromJ = fromCam.GetJstar();
double fromA = fromCam.GetAstar();
double fromB = fromCam.GetBstar();
double toJ = toCam.GetJstar();
double toA = toCam.GetAstar();
double toB = toCam.GetBstar();
double jstar = fromJ + (toJ - fromJ) * amount;
double astar = fromA + (toA - fromA) * amount;
double bstar = fromB + (toB - fromB) * amount;
return Cam16.FromUcs(jstar, astar, bstar).ToInt();
}
}