PHP - How to convert RGB color to CIE 1931 color s

2019-04-17 15:09发布

问题:

I am creating my own PHP based application where i want to change RGB color into xy format of CIE 1931.

How can i convert my RGB color specs to the CIE color space?

回答1:

First calculate X, Y and Z with the transform matrix and then normalize the result

X = 0.4124*R + 0.3576*G + 0.1805*B
Y = 0.2126*R + 0.7152*G + 0.0722*B
Z = 0.0193*R + 0.1192*G + 0.9505*B

Normalize:

x = X / (X + Y + Z)
y = Y / (X + Y + Z)


回答2:

Here is my Javascript version. It should help you enough ;)

 /**
 * Convert RGB to XY
 */
function RGBtoXY(red,green,blue){
    red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
    green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
    blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
    var X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
    var Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
    var Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
    var fx = X / (X + Y + Z);
    var fy = Y / (X + Y + Z);
    return [fx.toPrecision(4),fy.toPrecision(4)];
}