[reply]class ColourSwitch
{
public:
class sRGB
{
public:
double r = 0.0;
double g = 0.0;
double b = 0.0;
} sRGB;
class xyY
{
public:
double x = 0.0;
double y = 0.0;
double Y = 0.0;
} xyY;
class XYZ
{
public:
double X = 0.0;
double Y = 0.0;
double Z = 0.0;
} XYZ;
void XYZ2xyY()
{
xyY.Y = XYZ.Y;
xyY.x = XYZ.X / (XYZ.X + XYZ.Y + XYZ.Z);
xyY.y = XYZ.Y / (XYZ.X + XYZ.Y + XYZ.Z);
}
void xyY2XYZ()
{
XYZ.X = xyY.x * (xyY.Y / xyY.y);
XYZ.Y = xyY.Y;
XYZ.Z = (1 - xyY.x - xyY.y) * (xyY.Y / xyY.y);
}
void XYZ2sRGB()
{
//D65/2度
double _x = XYZ.X / 100;
double _y = XYZ.Y / 100;
double _z = XYZ.Z / 100;
double _r = _x * (+3.2406) + _y * (-1.5372) + _z * (-0.4986);
double _g = _x * (-0.9689) + _y * (+1.8758) + _z * (+0.0415);
double _b = _x * (+0.0557) + _y * (-0.2040) + _z * (+1.0570);
double tp = 1.0 / 2.4;
if (_r > 0.0031308)
{
_r = (+1.055) * (double)(pow(_r, tp)) - (+0.055);
}
else
{
_r = 12.92 * _r;
}
if (_g > 0.0031308)
{
_g = (+1.055f) * (double)(pow(_g, tp)) - (+0.055);
}
else
{
_g = 12.92f * _g;
}
if (_b > 0.0031308)
{
_b = (+1.055) * (double)(pow(_b, tp)) - (+0.055);
}
else
{
_b = 12.92 * _b;
}
sRGB.r = _r * 255.0;
sRGB.g = _g * 255.0;
sRGB.b = _b * 255.0;
sRGB.r = (sRGB.r < 0.0) ? 0.0 : sRGB.r;
sRGB.g = (sRGB.g < 0.0) ? 0.0 : sRGB.g;
sRGB.b = (sRGB.b < 0.0) ? 0.0 : sRGB.b;
sRGB.r = (sRGB.r > 255.0) ? 255.0 : sRGB.r;
sRGB.g = (sRGB.g > 255.0) ? 255.0 : sRGB.g;
sRGB.b = (sRGB.b > 255.0) ? 255.0 : sRGB.b;
}
void sRGB2XYZ()
{
double _r = sRGB.r / 255.0;
double _g = sRGB.g / 255.0;
double _b = sRGB.b / 255.0;
double tp = 2.4;
if (_r > 0.04045)
{
double rp = ((_r + 0.055) / 1.055);
_r = (double)(pow(rp, tp));
}
else
{
_r = _r / 12.92;
}
if (_g > 0.04045)
{
double gp = ((_g + 0.055) / 1.055);
_g = (double)(pow(gp, tp));
}
else
{
_g = _g / 12.92;
}
if (_b > 0.04045)
{
double bp = ((_b + 0.055) / 1.055);
long long* _bp = (long long*)&bp;
_b = (double)(pow(bp, tp));
}
else
{
_b = _b / 12.92;
}
_r = _r * 100.0;
_g = _g * 100.0;
_b = _b * 100.0;
XYZ.X = _r * (0.4124) + _g * (0.3576) + _b * (0.1805);
XYZ.Y = _r * (0.2126) + _g * (0.7152) + _b * (0.0722);
XYZ.Z = _r * (0.0193) + _g * (0.1192) + _b * (0.9505);
}
};
[/reply]
class ColourSwitch
{
public:
class sRGB
{
public:
double r = 0.0;
double g = 0.0;
double b = 0.0;
} sRGB;
class xyY
{
public:
double x = 0.0;
double y = 0.0;
double Y = 0.0;
} xyY;
class XYZ
{
public:
double X = 0.0;
double Y = 0.0;
double Z = 0.0;
} XYZ;
void XYZ2xyY()
{
xyY.Y = XYZ.Y;
xyY.x = XYZ.X / (XYZ.X + XYZ.Y + XYZ.Z);
xyY.y = XYZ.Y / (XYZ.X + XYZ.Y + XYZ.Z);
}
void xyY2XYZ()
{
XYZ.X = xyY.x * (xyY.Y / xyY.y);
XYZ.Y = xyY.Y;
XYZ.Z = (1 - xyY.x - xyY.y) * (xyY.Y / xyY.y);
}
void XYZ2sRGB()
{
//D65/2度
double _x = XYZ.X / 100;
double _y = XYZ.Y / 100;
double _z = XYZ.Z / 100;
double _r = _x * (+3.2406) + _y * (-1.5372) + _z * (-0.4986);
double _g = _x * (-0.9689) + _y * (+1.8758) + _z * (+0.0415);
double _b = _x * (+0.0557) + _y * (-0.2040) + _z * (+1.0570);
double tp = 1.0 / 2.4;
if (_r > 0.0031308)
{
_r = (+1.055) * (double)(pow(_r, tp)) - (+0.055);
}
else
{
_r = 12.92 * _r;
}
if (_g > 0.0031308)
{
_g = (+1.055f) * (double)(pow(_g, tp)) - (+0.055);
}
else
{
_g = 12.92f * _g;
}
if (_b > 0.0031308)
{
_b = (+1.055) * (double)(pow(_b, tp)) - (+0.055);
}
else
{
_b = 12.92 * _b;
}
sRGB.r = _r * 255.0;
sRGB.g = _g * 255.0;
sRGB.b = _b * 255.0;
sRGB.r = (sRGB.r < 0.0) ? 0.0 : sRGB.r;
sRGB.g = (sRGB.g < 0.0) ? 0.0 : sRGB.g;
sRGB.b = (sRGB.b < 0.0) ? 0.0 : sRGB.b;
sRGB.r = (sRGB.r > 255.0) ? 255.0 : sRGB.r;
sRGB.g = (sRGB.g > 255.0) ? 255.0 : sRGB.g;
sRGB.b = (sRGB.b > 255.0) ? 255.0 : sRGB.b;
}
void sRGB2XYZ()
{
double _r = sRGB.r / 255.0;
double _g = sRGB.g / 255.0;
double _b = sRGB.b / 255.0;
double tp = 2.4;
if (_r > 0.04045)
{
double rp = ((_r + 0.055) / 1.055);
_r = (double)(pow(rp, tp));
}
else
{
_r = _r / 12.92;
}
if (_g > 0.04045)
{
double gp = ((_g + 0.055) / 1.055);
_g = (double)(pow(gp, tp));
}
else
{
_g = _g / 12.92;
}
if (_b > 0.04045)
{
double bp = ((_b + 0.055) / 1.055);
long long* _bp = (long long*)&bp;
_b = (double)(pow(bp, tp));
}
else
{
_b = _b / 12.92;
}
_r = _r * 100.0;
_g = _g * 100.0;
_b = _b * 100.0;
XYZ.X = _r * (0.4124) + _g * (0.3576) + _b * (0.1805);
XYZ.Y = _r * (0.2126) + _g * (0.7152) + _b * (0.0722);
XYZ.Z = _r * (0.0193) + _g * (0.1192) + _b * (0.9505);
}
};