Beispiel #1
0
        public static CIE1931Point RgbToXY(RGBColor color, string model)
        {
            // Apply gamma correction. Convert non-linear RGB colour components
            // to linear color intensity levels.
            double r = InverseGamma(color.R);
            double g = InverseGamma(color.G);
            double b = InverseGamma(color.B);

            // Hue bulbs (depending on the type) can display colors outside the sRGB gamut supported
            // by most computer screens.
            // To make sure all colors are selectable by the user, Philips in its implementation
            // decided to interpret all RGB colors as if they were from a wide (non-sRGB) gamut.
            // The effect of this is to map colors in sRGB to a broader gamut of colors the hue lights
            // can produce.
            //
            // This also results in some deviation of color on screen vs color in real-life.
            //
            // The Philips implementation describes the matrix below with the comment
            // "Wide Gamut D65", but the values suggest this is infact not a standard
            // gamut but some custom gamut.
            //
            // The coordinates of this gamut have been identified as follows:
            //  red: (0.700607, 0.299301)
            //  green: (0.172416, 0.746797)
            //  blue: (0.135503, 0.039879)
            //
            // (substitute r = 1, g = 1, b = 1 in sequence into array below and convert
            //  from XYZ to xyY coordinates).
            // The plotted chart can be seen here: http://imgur.com/zelKnSk
            //
            // Also of interest, the white point is not D65 (0.31271, 0.32902), but a slightly
            // shifted version at (0.322727, 0.32902). This might be because true D65 is slightly
            // outside Gamut B (the position of D65 in the linked chart is slightly inaccurate).
            double X = r * 0.664511f + g * 0.154324f + b * 0.162028f;
            double Y = r * 0.283881f + g * 0.668433f + b * 0.047685f;
            double Z = r * 0.000088f + g * 0.072310f + b * 0.986039f;

            CIE1931Point xyPoint = new CIE1931Point(0.0, 0.0);

            if ((X + Y + Z) > 0.0)
            {
                // Convert from CIE XYZ to CIE xyY coordinates.
                xyPoint = new CIE1931Point(X / (X + Y + Z), Y / (X + Y + Z));
            }

            if (model != null)
            {
                //Check if the given XY value is within the colourreach of our lamps.
                CIE1931Gamut gamut = CIE1931Gamut.ForModel(model);

                // The point, adjusted it to the nearest point that is within the gamut of the lamp, if neccessary.
                return(gamut.NearestContainedPoint(xyPoint));
            }
            return(xyPoint);
        }
Beispiel #2
0
        public static RGBColor XYToRgb(CIE1931Point point, string model)
        {
            if (model != null)
            {
                CIE1931Gamut gamut = CIE1931Gamut.ForModel(model);

                // If the color is outside the lamp's gamut, adjust to the nearest color
                // inside the lamp's gamut.
                point = gamut.NearestContainedPoint(point);
            }

            // Also adjust it to be in the Philips "Wide Gamut" if not already.
            // The wide gamut used for XYZ->RGB conversion does not quite contain all colors
            // all of the hue bulbs support.
            // ReSharper disable once ImpureMethodCallOnReadonlyValueField
            point = CIE1931Gamut.PhilipsWideGamut.NearestContainedPoint(point);

            // Convert from xyY to XYZ coordinates.
            double Y = 1.0;             // Luminance
            double X = (Y / point.y) * point.x;
            double Z = (Y / point.y) * point.z;

            // The Philips implementation comments this matrix with "sRGB D65 conversion"
            // However, this is not the XYZ -> RGB conversion matrix for sRGB. Instead
            // the matrix that is the inverse of that in RgbToXY() is used.
            // See comment in RgbToXY() for more info.
            double r = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
            double g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
            double b = X * 0.051713 - Y * 0.121364 + Z * 1.011530;

            // Downscale color components so that largest component has an intensity of 1.0,
            // as we can't display colors brighter than that.
            double maxComponent = Math.Max(Math.Max(r, g), b);

            if (maxComponent > 1.0)
            {
                r /= maxComponent;
                g /= maxComponent;
                b /= maxComponent;
            }

            // We now have the (linear) amounts of R, G and B corresponding to the specified XY coordinates.
            // Since displays are non-linear, we must apply a gamma correction to get the pixel value.
            // For example, a pixel red value of 1.0 (255) is more than twice as bright as 0.5 (127).
            // We need to correct for this non-linearity.
            r = Gamma(r);
            g = Gamma(g);
            b = Gamma(b);

            // Philips applies a second round of downscaling here, but that should be unnecessary given
            // gamma returns a value between 0.0 and 1.0 for every input between 0.0 and 1.0.
            return(new RGBColor(r, g, b));
        }