RGB转Dec
/// <summary>
/// RGB转Dec
/// </summary>
/// <returns></returns>
public static int RGBToDec(int r, int g, int b)
{
return 65536 * b + 256 * g + r;
}
Dec转RGB
/// <summary>
/// Dec转RGB
/// </summary>
public static void DecToRgb(int dec, out int R, out int G, out int B)
{
R = dec & 255;
G = (dec & 65280) / 256;
B = (dec & 16711680) / 65536;
}
RGB转HEX
/// <summary>
/// RGB转HEX
/// </summary>
/// <returns></returns>
public static string RGBToHEX(int R, int G, int B)
{
string r = Convert.ToString(R, 16).ToUpper();
string g = Convert.ToString(G, 16).ToUpper();
string b = Convert.ToString(B, 16).ToUpper();
return "#" + r + g + b;
}
HEX转RGB
/// <summary>
/// HEX转RGB
/// </summary>
public static void HEXToRGB(string HEX, out int R, out int G, out int B)
{
R = Convert.ToInt32(HEX.Substring(1, 2),16);
G = Convert.ToInt32(HEX.Substring(3, 2),16);
B = Convert.ToInt32(HEX.Substring(5, 2),16);
}
RGB转HSB
/// <summary>
/// RGB转HSB
/// </summary>
public static void RGBToHSB(int red, int green, int blue, out int hue, out int sat, out int bri)
{
double r = ((double)red / 255.0);
double g = ((double)green / 255.0);
double b = ((double)blue / 255.0);
double max = Math.Max(r, Math.Max(g, b));
double min = Math.Min(r, Math.Min(g, b));
double temp = 0 ;
if (max == r && g >= b)
{
if (max - min == 0)
{
hue = 0;
}
else
{
temp = (60 * (g - b) / (max - min));
}
}
else if (max == r && g < b)
{
temp = (60 * (g - b) / (max - min) + 360);
}
else if (max == g)
{
temp = (60 * (b - r) / (max - min) + 120);
}
else if (max == b)
{
temp = (60 * (r - g) / (max - min) + 240);
}
hue = (int)Math.Round(temp,2);
temp = (((max == 0) ? 0.0 : (1.0 - ((double)min / (double)max))) * 100);
sat = (int)Math.Round(temp,2);
bri = (int)Math.Round(max * 100,2);
}
RGB转ESL
/// <summary>
/// RGB转ESL
/// </summary>
/// <param name="R"></param>
/// <param name="G"></param>
/// <param name="B"></param>
/// <param name="e"></param>
/// <param name="s"></param>
/// <param name="l"></param>
public static void RGBToESL(int R, int G, int B, out int E, out int S, out int L)
{
float r = (float)R / 255;
float g = (float)G / 255;
float b = (float)B / 255;
float minVal, maxVal;
float _h, _s, _l;
if (r > g)
{
maxVal = r;
minVal = g;
}
else
{
maxVal = g;
minVal = r;
}
if (b > maxVal)
{
maxVal = b;
}
if (b < minVal)
{
minVal = b;
}
if (maxVal == minVal)
{
_h = 0;
}
else if (maxVal == r && g >= b)
{
_h = 60 * (g - b) / (maxVal - minVal);
}
else if (maxVal == r && g < b)
{
_h = 60 * (g - b) / (maxVal - minVal) + 360;
}
else if (maxVal == g)
{
_h = 60 * (b - r) / (maxVal - minVal) + 120;
}
else if (maxVal == b)
{
_h = 60 * (r - g) / (maxVal - minVal) + 240;
}
else
{
_h = 0;
}
// luminance
_l = (maxVal + minVal) / 2;
if (_l == 0 || maxVal == minVal)
{
_s = 0;
}
else if (0 < _l && _l <= 0.5)
{
_s = (maxVal - minVal) / (maxVal + minVal);
}
else if (_l > 0.5)
{
_s = (maxVal - minVal) / (2 - (maxVal + minVal));
}
else
{
_s = 0;
}
if (_h > 360)
{
_h = 360;
}
else if (_h < 0)
{
_h = 0;
}
if (_s > 1)
{
_s = 1;
}
else if (_s < 0)
{
_s = 0;
}
_s = _s * 100;
if (_l > 1)
{
_l = 1;
}
else if (_l < 0)
{
_l = 0;
}
_l = _l * 100;
E = (int)Math.Round(240 * _h / 360);
S = (int)Math.Round(240 * _s / 100);
L = (int)Math.Round(240 * _l / 100);
return;
}
RGB转反色
/// <summary>
/// 取RGB反色
/// </summary>
public static void RGBToAnia(int r,int g, int b,out int R,out int G,out int B)
{
R = 255 - r;
G = 255 - g;
B = 255 - b;
}
RGB转Lab
/// <summary>
/// RGB转LAB,2°D65的光源,保留小数点2位
/// </summary>
public static void RGBToLab(int Red, int Green, int Blue, out double L, out double A, out double B)
{
double X, Y, Z;
Internal_RGB2XYZ(Red, Green, Blue, out X, out Y, out Z);
double l, a, b;
Intrrnal_XYZ2LAB(X, Y, Z, out l, out a, out b);
L = Math.Round(l, 2);
A = Math.Round(a, 2);
B = Math.Round(b, 2);
return;
}
XYZ转Lab
/// <summary>
/// XYZ转Lab,2°D65光源,保留小数点2位
/// </summary>
public static void XYZToLab(double X,double Y,double Z, out double L,out double A,out double B)
{
double x = X / 100;
double y = Y / 100;
double z = Z / 100;
double l, a, b;
Intrrnal_XYZ2LAB(x , y , z , out l , out a , out b);
L = Math.Round(l,2);
A = Math.Round(a,2);
B = Math.Round(b,2);
}
/// <summary>
/// RGB转XYZ,2°D65的光源,保留小数点后2位
/// </summary>
public static void RGBToXYZ(int Red, int Green, int Blue, out double X, out double Y, out double Z)
{
double x, y, z;
Internal_RGB2XYZ(Red, Green, Blue, out x, out y, out z);
X = Math.Round(x * 100, 2);
Y = Math.Round(y * 100, 2);
Z = Math.Round(z * 100, 2);
return;
}
/// <summary>
/// 内部
/// </summary>
private static void Intrrnal_XYZ2LAB(double X, double Y, double Z, out double L, out double a, out double b)
{
const float param_13 = 1.0f / 3.0f;
const float param_16116 = 16.0f / 116.0f;
const float Xn = 0.950456f;
const float Yn = 1.0f;
const float Zn = 1.088754f;
double fX, fY, fZ;
double X1, Y1, Z1;
X1 = X / (Xn);
Y1 = Y / (Yn);
Z1 = Z / (Zn);
if (Y1 > 0.008856f)
fY = (float)Math.Pow(Y1, param_13);
else
fY = 7.787f * Y1 + param_16116;
if (X1 > 0.008856f)
fX = (float)Math.Pow(X1, param_13);
else
fX = 7.787f * X1 + param_16116;
if (Z1 > 0.008856)
fZ = (float)Math.Pow(Z1, param_13);
else
fZ = 7.787f * Z1 + param_16116;
L = 116.0f * fY - 16.0f;
L = L > 0.0f ? L : 0.0f;
a = 500.0f * (fX - fY);
b = 200.0f * (fY - fZ);
}
RGB转XYZ
/// <summary>
/// RGB转XYZ,2°D65的光源,保留小数点后2位
/// </summary>
public static void RGBToXYZ(int Red, int Green, int Blue, out double X, out double Y, out double Z)
{
double x, y, z;
Internal_RGB2XYZ(Red, Green, Blue, out x, out y, out z);
X = Math.Round(x * 100, 2);
Y = Math.Round(y * 100, 2);
Z = Math.Round(z * 100, 2);
return;
}
/// <summary>
/// 内部
/// </summary>
private static void Internal_RGB2XYZ(int Red, int Green, int Blue, out double X, out double Y, out double Z)
{
double R = Red / 255F > 0.04045 ? Math.Pow((Red / 255F + 0.055f) / 1.055f, 2.4f) : (Red / 255F / 12.92);
double G = Green / 255F > 0.04045 ? Math.Pow((Green / 255F + 0.055f) / 1.055f, 2.4f) : (Green / 255F / 12.92);
double B = Blue / 255F > 0.04045 ? Math.Pow((Blue / 255F + 0.055f) / 1.055f, 2.4f) : (Blue / 255F / 12.92);
X = 0.4124564f * R + 0.3575761f * G + 0.1804375f * B;
Y = 0.2126729f * R + 0.7151522f * G + 0.0721750f * B;
Z = 0.0193339f * R + 0.1191920f * G + 0.9503041f * B;
}