|
1 |
| -using System.Drawing; |
| 1 | +using System; |
| 2 | +using System.Drawing; |
2 | 3 |
|
3 | 4 | namespace CodeStream.VisualStudio.Extensions
|
4 | 5 | {
|
5 |
| - public static class ColorExtensions |
6 |
| - { |
7 |
| - public static System.Windows.Media.Color ConvertToMediaColor(this System.Drawing.Color color) |
8 |
| - { |
9 |
| - return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B); |
10 |
| - } |
| 6 | + public static class ColorExtensions |
| 7 | + { |
| 8 | + public static System.Windows.Media.Color ConvertToMediaColor(this System.Drawing.Color color) |
| 9 | + { |
| 10 | + return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B); |
| 11 | + } |
11 | 12 |
|
12 |
| - public static string ToHex(this Color color) |
13 |
| - { |
14 |
| - return "#"+ color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"); |
15 |
| - } |
| 13 | + public static string ToRgba(this Color color) |
| 14 | + { |
| 15 | + return $"rgba({color.R}, {color.G}, {color.B}, {(double)color.A / 255})"; |
| 16 | + } |
16 | 17 |
|
17 |
| - public static string ToArgb(this Color color, double percentage = 100) |
18 |
| - { |
19 |
| - return $"rgba({color.R}, {color.G}, {color.B}, {(percentage / 100)})"; |
20 |
| - } |
| 18 | + public static float Lerp(this float start, float end, float amount) |
| 19 | + { |
| 20 | + float difference = end - start; |
| 21 | + float adjusted = difference * amount; |
| 22 | + return start + adjusted; |
| 23 | + } |
21 | 24 |
|
22 |
| - public static float Lerp(this float start, float end, float amount) |
23 |
| - { |
24 |
| - float difference = end - start; |
25 |
| - float adjusted = difference * amount; |
26 |
| - return start + adjusted; |
27 |
| - } |
| 25 | + /// <summary> |
| 26 | + /// https://stackoverflow.com/questions/97646/how-do-i-determine-darker-or-lighter-color-variant-of-a-given-color/2690026#2690026 |
| 27 | + /// </summary> |
| 28 | + /// <param name="color"></param> |
| 29 | + /// <param name="to"></param> |
| 30 | + /// <param name="amount"></param> |
| 31 | + /// <returns></returns> |
| 32 | + public static Color Lerp(this Color color, Color to, float amount) |
| 33 | + { |
| 34 | + // start colours as lerp-able floats |
| 35 | + float sr = color.R, sg = color.G, sb = color.B; |
28 | 36 |
|
29 |
| - /// <summary> |
30 |
| - /// https://stackoverflow.com/questions/97646/how-do-i-determine-darker-or-lighter-color-variant-of-a-given-color/2690026#2690026 |
31 |
| - /// </summary> |
32 |
| - /// <param name="color"></param> |
33 |
| - /// <param name="to"></param> |
34 |
| - /// <param name="amount"></param> |
35 |
| - /// <returns></returns> |
36 |
| - public static Color Lerp(this Color color, Color to, float amount) |
37 |
| - { |
38 |
| - // start colours as lerp-able floats |
39 |
| - float sr = color.R, sg = color.G, sb = color.B; |
| 37 | + // end colours as lerp-able floats |
| 38 | + float er = to.R, eg = to.G, eb = to.B; |
40 | 39 |
|
41 |
| - // end colours as lerp-able floats |
42 |
| - float er = to.R, eg = to.G, eb = to.B; |
| 40 | + // lerp the colours to get the difference |
| 41 | + byte r = (byte)sr.Lerp(er, amount), |
| 42 | + g = (byte)sg.Lerp(eg, amount), |
| 43 | + b = (byte)sb.Lerp(eb, amount); |
43 | 44 |
|
44 |
| - // lerp the colours to get the difference |
45 |
| - byte r = (byte)sr.Lerp(er, amount), |
46 |
| - g = (byte)sg.Lerp(eg, amount), |
47 |
| - b = (byte)sb.Lerp(eb, amount); |
| 45 | + // return the new colour |
| 46 | + return Color.FromArgb(r, g, b); |
| 47 | + } |
48 | 48 |
|
49 |
| - // return the new colour |
50 |
| - return Color.FromArgb(r, g, b); |
51 |
| - } |
| 49 | + /// <summary> |
| 50 | + /// Darken a color |
| 51 | + /// </summary> |
| 52 | + /// <param name="color"></param> |
| 53 | + /// <param name="rate">0.1f == 10%</param> |
| 54 | + /// <param name="darker"></param> |
| 55 | + /// <returns></returns> |
| 56 | + public static Color Darken(this Color color, float rate = 0.1f, Color? darker = null) |
| 57 | + { |
| 58 | + return color.Lerp(darker ?? Color.Black, rate); |
| 59 | + } |
52 | 60 |
|
53 |
| - /// <summary> |
54 |
| - /// Darken a color |
55 |
| - /// </summary> |
56 |
| - /// <param name="color"></param> |
57 |
| - /// <param name="rate">0.1f == 10%</param> |
58 |
| - /// <param name="darker"></param> |
59 |
| - /// <returns></returns> |
60 |
| - public static Color Darken(this Color color, float rate = 0.1f, Color? darker = null) |
61 |
| - { |
62 |
| - return color.Lerp(darker ?? Color.Black, rate); |
63 |
| - } |
| 61 | + /// <summary> |
| 62 | + /// Lighten a color |
| 63 | + /// </summary> |
| 64 | + /// <param name="color"></param> |
| 65 | + /// <param name="rate">0.1f == 10%</param> |
| 66 | + /// <param name="lighter"></param> |
| 67 | + /// <returns></returns> |
| 68 | + public static Color Lighten(this Color color, float rate = 0.1f, Color? lighter = null) |
| 69 | + { |
| 70 | + return color.Lerp(lighter ?? Color.White, rate); |
| 71 | + } |
64 | 72 |
|
65 |
| - /// <summary> |
66 |
| - /// Lighten a color |
67 |
| - /// </summary> |
68 |
| - /// <param name="color"></param> |
69 |
| - /// <param name="rate">0.1f == 10%</param> |
70 |
| - /// <param name="lighter"></param> |
71 |
| - /// <returns></returns> |
72 |
| - public static Color Lighten(this Color color, float rate = 0.1f, Color? lighter = null) |
73 |
| - { |
74 |
| - return color.Lerp(lighter ?? Color.White, rate); |
75 |
| - } |
| 73 | + public static Color Opacity(this Color color, double percentage = 100) |
| 74 | + { |
| 75 | + return Color.FromArgb(Convert.ToInt32(255 * (color.A / 255 * percentage / 100)), color); |
| 76 | + } |
76 | 77 |
|
77 |
| - /// <summary> |
78 |
| - /// Returns whether a color is closer to black rather than white |
79 |
| - /// </summary> |
80 |
| - /// <param name="c2"></param> |
81 |
| - /// <remarks>https://stackoverflow.com/a/9780689/208022</remarks> |
82 |
| - /// <returns></returns> |
83 |
| - public static bool IsDark(this Color c2) |
84 |
| - { |
85 |
| - return (0.2126 * c2.R + 0.7152 * c2.G + 0.0722 * c2.B) < 128; |
86 |
| - } |
87 |
| - } |
| 78 | + /// <summary> |
| 79 | + /// Returns whether a color is closer to black rather than white |
| 80 | + /// </summary> |
| 81 | + /// <param name="c2"></param> |
| 82 | + /// <remarks>https://stackoverflow.com/a/9780689/208022</remarks> |
| 83 | + /// <returns></returns> |
| 84 | + public static bool IsDark(this Color c2) |
| 85 | + { |
| 86 | + return (0.2126 * c2.R + 0.7152 * c2.G + 0.0722 * c2.B) < 128; |
| 87 | + } |
| 88 | + } |
88 | 89 | }
|
0 commit comments