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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| using System;
using System.Drawing;
namespace RMA.Drawing
{
public class ColorRGB
{
public byte R;
public byte G;
public byte B;
public byte A;
public ColorRGB()
{
R = 255;
G = 255;
B = 255;
A = 255;
}
public ColorRGB(Color value)
{
this.R = value.R;
this.G = value.G;
this.B = value.B;
this.A = value.A;
}
public static implicit operator Color(ColorRGB rgb)
{
Color c = Color.FromArgb(rgb.A, rgb.R, rgb.G, rgb.B);
return c;
}
public static explicit operator ColorRGB(Color c)
{
return new ColorRGB(c);
}
// Given H,S,L in range of 0-1
// Returns a Color (RGB struct) in range of 0-255
public static ColorRGB FromHSL(double H, double S, double L)
{
return FromHSLA(H, S, L, 1.0);
}
// Given H,S,L,A in range of 0-1
// Returns a Color (RGB struct) in range of 0-255
public static ColorRGB FromHSLA(double H, double S, double L, double A)
{
double v;
double r, g, b;
if (A > 1.0)
A = 1.0;
r = L; // default to gray
g = L;
b = L;
v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S);
if (v > 0)
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;
m = L + L - v;
sv = (v - m) / v;
H *= 6.0;
sextant = (int)H;
fract = H - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant)
{
case 0:
r = v;
g = mid1;
b = m;
break;
case 1:
r = mid2;
g = v;
b = m;
break;
case 2:
r = m;
g = v;
b = mid1;
break;
case 3:
r = m;
g = mid2;
b = v;
break;
case 4:
r = mid1;
g = m;
b = v;
break;
case 5:
r = v;
g = m;
b = mid2;
break;
}
}
ColorRGB rgb = new ColorRGB();
rgb.R = Convert.ToByte(r * 255.0f);
rgb.G = Convert.ToByte(g * 255.0f);
rgb.B = Convert.ToByte(b * 255.0f);
rgb.A = Convert.ToByte(A * 255.0f);
return rgb;
}
// Hue in range from 0.0 to 1.0
public float H
{
get
{
// Use System.Drawing.Color.GetHue, but divide by 360.0F
// because System.Drawing.Color returns hue in degrees (0 - 360)
// rather than a number between 0 and 1.
return ((Color)this).GetHue() / 360.0F;
}
}
// Saturation in range 0.0 - 1.0
public float S
{
get
{
return ((Color)this).GetSaturation();
}
}
// Lightness in range 0.0 - 1.0
public float L
{
get
{
return ((Color)this).GetBrightness();
}
}
}
} |