/// <summary>
/// 色相,饱和度,亮度转换成rgb值
/// </summary>
/// <returns></returns>
public static float[] HSB2RGB(float[] hsb)
{
if (hsb[] == )
{
hsb[] = ;
}
float[] rgb = new float[];
float r = ;
float g = ;
float b = ;
if (hsb[] == )
{
r = g = b = hsb[];
}
else
{
float sectorPos = hsb[] / 60f;
int sectorNum = (int)Math.Floor(sectorPos);
float fractionalSector = sectorPos - sectorNum;
float p = hsb[] * ( - hsb[]);
float q = hsb[] * ( - (hsb[] * fractionalSector));
float t = hsb[] * ( - (hsb[] * ( - fractionalSector)));
switch (sectorNum)
{
case :
r = hsb[];
g = t;
b = p;
break;
case :
r = q;
g = hsb[];
b = p;
break;
case :
r = p;
g = hsb[];
b = t;
break;
case :
r = p;
g = q;
b = hsb[];
break;
case :
r = t;
g = p;
b = hsb[];
break;
case :
r = hsb[];
g = p;
b = q;
break;
}
}
return new float[] { r * , g * , b * };
}
/// <summary>
/// 将rgb类型的颜色转换为hsb
/// </summary>
/// <param name="rgb"></param>
/// <returns></returns>
public static float[] RGB2HSB(float[] rgb)
{
float[] hsb = new float[];
float r = rgb[];
float g = rgb[];
float b = rgb[];
float max = Math.Max(r, Math.Max(g, b));
if (max <= )
{
return hsb;
}
float min = Math.Min(r, Math.Min(g, b));
float dif = max - min;
if (max > min)
{
if (g == max)
{
hsb[] = (b - r) / dif * 60f + 120f;
}
else if (b == max)
{
hsb[] = (r - g) / dif * 60f + 240f;
}
else if (b > g)
{
hsb[] = (g - b) / dif * 60f + 360f;
}
else
{
hsb[] = (g - b) / dif * 60f;
}
if (hsb[] < )
{
hsb[] = hsb[] + 360f;
}
}
else
{
hsb[] = ;
}
hsb[] = dif / max;
hsb[] = max / 255f;
return hsb;
}