public void ClampedColor_MultiplySaturationBy2ToOverSaturateThenDivideBy2_ReturnsOriginalColor()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new ClampedColor(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.Saturation /= 2;
            Console.WriteLine(color.ToString());
            Assert.That(color.Saturation, Is.EqualTo(0.5).Within(0.0000001));
        }
        public void Color_ClampingManuallyOverSaturatedColor_ReturnsUnchangedHue()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new Color(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.ClampExceedingColors();
            Console.WriteLine(color.ToString());
            Assert.That(color.Hue, Is.EqualTo(sourceColor.Hue).Within(0.000001));
        }
        public void Color_MultiplySaturationBy2ToOverSaturateThenDivideBy2_ReturnsOriginalColor()
        {
            var sourceColor = HexRgbUtil.HexStringToColor("2b6ca2", "rrggbb");
            var color       = new Color(sourceColor);

            color.Saturation *= 2;
            Console.WriteLine(color.ToString());
            color.Saturation /= 2;
            Console.WriteLine(color.ToString());
            Assert.True(color.Equals(sourceColor));
        }
Esempio n. 4
0
        /// <summary>
        /// Converts RGB hex string to Color. Applies padding to short strings by the color scheme padding rules.
        /// </summary>
        /// <param name="rgbString"></param>
        /// <param name="formats"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static Color PaddableHexStringToColor(string rgbString, IEnumerable <PaddableHexFormat> formats)
        {
            foreach (var format in formats)
            {
                string rgbHexFormat = format.HexFormat;
                if (format.Padding != null && format.Padding.Length != format.HexFormat.Length)
                {
                    throw new Exception("RGB hex string misconfiguration: " + format.HexFormat + " with padding " +
                                        format.Padding);
                }

                if (HexRgbUtil.IsValidHexString(rgbString) && rgbString.Length <= rgbHexFormat.Length)
                {
                    if (rgbString.Length < rgbHexFormat.Length)
                    {
                        if (format.PaddingDirection == PaddingDirection.Left ||
                            format.PaddingDirection == PaddingDirection.Right)
                        {
                            switch (format.PaddingDirection)
                            {
                            case PaddingDirection.Left:
                                rgbString = rgbString.PadLeft(format.Padding);
                                break;

                            case PaddingDirection.Right:
                                rgbString = rgbString.PadRight(format.Padding);
                                break;
                            }
                        }
                    }

                    if (rgbString.Length == rgbHexFormat.Length)
                    {
                        return(HexRgbUtil.HexStringToColor(rgbString, rgbHexFormat));
                    }
                }
            }

            throw new Exception("Invalid color string: " + rgbString);
        }