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
|
using System;
using System.Drawing;
namespace HSL
{
///<summary>
/// Description résumée de Class1.
///</summary>
publicclass HSL
{
privatefloat h;
privatefloat s;
privatefloat l;
publicfloat Hue
{
get
{
return h;
}
set
{
h = (float)(Math.Abs(value)%360);
}
}
publicfloat Saturation
{
get
{
return s;
}
set
{
s = (float)Math.Max(Math.Min(1.0, value), 0.0);
}
}
publicfloat Luminance
{
get
{
return l;
}
set
{
l = (float)Math.Max(Math.Min(1.0, value), 0.0);
}
}
private HSL()
{
}
public HSL(float hue, float saturation, float luminance)
{
Hue = hue;
Saturation = saturation;
Luminance = luminance;
}
public Color RGB
{
get
{
double r=0,g=0,b=0;
double temp1,temp2;
double normalisedH = h/360.0;
if(l==0)
{
r=g=b=0;
}
else
{
if(s==0)
{
r=g=b=l;
}
else
{
temp2 = ((l<=0.5) ? l*(1.0+s) : l+s-(l*s));
temp1 = 2.0*l-temp2;
double[] t3=newdouble[]{normalisedH+1.0/3.0,normalisedH,normalisedH-1.0/3.0};
double[] clr=newdouble[]{0,0,0};
for(int i=0;i<3;++i)
{
if(t3[i]<0)
t3[i]+=1.0;
if(t3[i]>1)
t3[i]-=1.0;
if(6.0*t3[i] < 1.0)
clr[i]=temp1+(temp2-temp1)*t3[i]*6.0;
elseif(2.0*t3[i] < 1.0)
clr[i]=temp2;
elseif(3.0*t3[i] < 2.0)
clr[i]=(temp1+(temp2-temp1)*((2.0/3.0)-t3[i])*6.0);
else
clr[i]=temp1;
}
r=clr[0];
g=clr[1];
b=clr[2];
}
}
return Color.FromArgb((int)(255*r),(int)(255*g),(int)(255*b));
}
}
privatestaticbyte toRGB(float rm1, float rm2, float rh)
{
if(rh>360) rh-=360;
elseif(rh< 0) rh+=360;
if(rh< 60) rm1=rm1+(rm2-rm1)*rh/60;
elseif(rh<180) rm1=rm2;
elseif(rh<240) rm1=rm1+(rm2-rm1)*(240-rh)/60;
return (byte)(rm1*255);
}
publicstatic HSL FromRGB(byte red, byte green, byte blue)
{
return FromRGB(Color.FromArgb(red, green, blue));
}
publicstatic HSL FromRGB(Color c)
{
returnnew HSL(c.GetHue(), c.GetSaturation(), c.GetBrightness());
}
}
}
|
Partager