Used to convert colors between XY and RGB internal: Do not expose
Esempio n. 1
0
        public static LightCommand SetColor(this LightCommand lightCommand, RGBColor color, string model = "LCT001")
        {
            if (lightCommand == null)
            {
                throw new ArgumentNullException(nameof(lightCommand));
            }

            var point = HueColorConverter.CalculateXY(color, model);

            return(lightCommand.SetColor(point.x, point.y));
        }
        /// <summary>
        ///  Method to see if the given XY value is within the reach of the lamps.
        /// </summary>
        /// <param name="p">p the point containing the X,Y value</param>
        /// <returns>true if within reach, false otherwise.</returns>
        private static bool CheckPointInLampsReach(CGPoint p)
        {
            CGPoint v1 = new CGPoint(Lime.x - Red.x, Lime.y - Red.y);
            CGPoint v2 = new CGPoint(Blue.x - Red.x, Blue.y - Red.y);

            CGPoint q = new CGPoint(p.x - Red.x, p.y - Red.y);

            double s = HueColorConverter.CrossProduct(q, v2) / HueColorConverter.CrossProduct(v1, v2);
            double t = HueColorConverter.CrossProduct(v1, q) / HueColorConverter.CrossProduct(v1, v2);

            if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
 public static string ToHex(this Light light)
 {
     return(HueColorConverter.HexFromState(light.State, light.ModelId));
 }
 public static RGBColor ToRGBColor(this Light light)
 {
     return(HueColorConverter.RGBColorFromState(light.State, light.ModelId));
 }
        /// <summary>
        ///  Get XY from red,green,blue ints
        /// </summary>
        /// <param name="red"></param>
        /// <param name="green"></param>
        /// <param name="blue"></param>
        /// <returns></returns>
        public static CGPoint XyFromColor(int red, int green, int blue)
        {
            double r = (red > 0.04045f) ? Math.Pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
            double g = (green > 0.04045f) ? Math.Pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
            double b = (blue > 0.04045f) ? Math.Pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);

            double X = r * 0.4360747f + g * 0.3850649f + b * 0.0930804f;
            double Y = r * 0.2225045f + g * 0.7168786f + b * 0.0406169f;
            double Z = r * 0.0139322f + g * 0.0971045f + b * 0.7141733f;

            double cx = X / (X + Y + Z);
            double cy = Y / (X + Y + Z);

            if (Double.IsNaN(cx))
            {
                cx = 0.0f;
            }

            if (Double.IsNaN(cy))
            {
                cy = 0.0f;
            }

            //Check if the given XY value is within the colourreach of our lamps.
            CGPoint xyPoint        = new CGPoint(cx, cy);
            bool    inReachOfLamps = HueColorConverter.CheckPointInLampsReach(xyPoint);

            if (!inReachOfLamps)
            {
                //It seems the colour is out of reach
                //let's find the closes colour we can produce with our lamp and send this XY value out.

                //Find the closest point on each line in the triangle.
                CGPoint pAB = HueColorConverter.GetClosestPointToPoint(Red, Lime, xyPoint);
                CGPoint pAC = HueColorConverter.GetClosestPointToPoint(Blue, Red, xyPoint);
                CGPoint pBC = HueColorConverter.GetClosestPointToPoint(Lime, Blue, xyPoint);

                //Get the distances per point and see which point is closer to our Point.
                double dAB = HueColorConverter.GetDistanceBetweenTwoPoints(xyPoint, pAB);
                double dAC = HueColorConverter.GetDistanceBetweenTwoPoints(xyPoint, pAC);
                double dBC = HueColorConverter.GetDistanceBetweenTwoPoints(xyPoint, pBC);

                double  lowest       = dAB;
                CGPoint closestPoint = pAB;

                if (dAC < lowest)
                {
                    lowest       = dAC;
                    closestPoint = pAC;
                }
                if (dBC < lowest)
                {
                    lowest       = dBC;
                    closestPoint = pBC;
                }

                //Change the xy value to a value which is within the reach of the lamp.
                cx = closestPoint.x;
                cy = closestPoint.y;
            }

            return(new CGPoint(cx, cy));
        }
 public static RGBColor ToRGBColor(this State state, string model = "LCT001")
 {
     return(HueColorConverter.RGBColorFromState(state, model));
 }
 public static string ToHex(this State state, string model = "LCT001")
 {
     return(HueColorConverter.HexColorFromState(state, model));
 }