Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColorF(HsbColor32 value)
 {
     _hue        = value.Hue.ToDegrees();
     _saturation = value.Saturation.ToPercentage();
     _brightness = value.Brightness.ToPercentage();
     _alpha      = value.Alpha.ToPercentage();
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColor32Normalized(HsbColor32 value)
 {
     _value = 0;
     ColorExtensions.HSBtoRGB(value.Hue.ToDegrees(), value.Saturation.ToPercentage(), value.Brightness.ToPercentage(), out float r, out float g, out float b);
     ColorExtensions.RGBtoHSB(r, g, b, out float hF, out float sF, out float bF);
     _hue        = hF.FromDegrees();
     _brightness = bF.FromPercentage();
     _saturation = sF.FromPercentage();
     _alpha      = value.Alpha;
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryGetColor(PSObject obj, out IColorModel result)
        {
            if (obj != null)
            {
                object baseObj = obj.BaseObject;
                if (baseObj is IColorModel)
                {
                    result = (IColorModel)baseObj;
                    return(true);
                }
                string[]       names    = new string[] { "Hue", "Saturation", "Brightness" };
                StringComparer comparer = StringComparer.InvariantCultureIgnoreCase;
                object         alpha    = obj.Properties.Where(p => comparer.Equals(p.Name, "Alpha") && p.IsInstance && p.IsGettable)
                                          .Select(p => AsSimplestType(p.Value)).Where(o => o != null && (o is float || o is byte)).ToArray();

                object[] hsb = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                               .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                if (hsb.Length != 3)
                {
                    names = new string[] { "Hue", "Saturation", "Lightness" };
                    hsb   = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                            .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                    if (hsb.Length != 3)
                    {
                        names = new string[] { "H", "S", "L" };
                        hsb   = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                                .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                        if (hsb.Length != 3)
                        {
                            names = new string[] { "H", "S", "B" };
                            hsb   = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                                    .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                        }
                    }
                }
                if (hsb.Length == 3)
                {
                    if (hsb[0] is byte)
                    {
                        if (hsb[1] is byte && hsb[2] is byte)
                        {
                            if (alpha == null)
                            {
                                result = new HsbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2]);
                                return(true);
                            }
                            if (alpha is byte)
                            {
                                result = new HsbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2], (byte)alpha);
                                return(true);
                            }
                        }
                    }
                    else if (hsb[0] is float h && h >= 0f && h <= 360f && hsb[1] is byte s && s >= 0f && s <= 1f && hsb[2] is byte b && b >= 0f && b <= 1f)
                    {
                        if (alpha == null)
                        {
                            result = new HsbColorF(h, s, b);
                            return(true);
                        }
                        if (alpha is float && (float)alpha >= 0f && (float)alpha <= 1f)
                        {
                            result = new HsbColorF(h, s, b, (float)alpha);
                            return(true);
                        }
                    }
                }
                names = new string[] { "Red", "Green", "Blue" };
                hsb   = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                        .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                if (hsb.Length != 3)
                {
                    names = new string[] { "R", "G", "B" };
                    hsb   = names.Select(n => obj.Properties.FirstOrDefault(p => comparer.Equals(p.Name, n))).Where(p => p != null && p.IsInstance && p.IsGettable)
                            .Select(p => AsSimplestType(p.Value)).Where(o => o != null).ToArray();
                }
                if (hsb.Length == 3)
                {
                    if (hsb[0] is byte)
                    {
                        if (hsb[1] is byte && hsb[2] is byte)
                        {
                            if (alpha == null)
                            {
                                result = new RgbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2]);
                                return(true);
                            }
                            if (alpha is byte)
                            {
                                result = new RgbColor32((byte)hsb[0], (byte)hsb[1], (byte)hsb[2], (byte)alpha);
                                return(true);
                            }
                        }
                    }
                    else if (hsb[0] is float h && h >= 0f && h <= 360f && hsb[1] is byte s && s >= 0f && s <= 1f && hsb[2] is byte b && b >= 0f && b <= 1f)
                    {
                        if (alpha == null)
                        {
                            result = new RgbColorF(h, s, b);
                            return(true);
                        }
                        if (alpha is float && (float)alpha >= 0f && (float)alpha <= 1f)
                        {
                            result = new RgbColorF(h, s, b, (float)alpha);
                            return(true);
                        }
                    }
                }
            }
            result = null;
            return(false);
        }
        /// <summary>
        /// Gets formatted string representing the current color value.
        /// </summary>
        /// <param name="format">The color string format to use.</param>
        /// <returns>The formatted string representing the current color value.</returns>
        public string ToString(ColorStringFormat format)
        {
            float r, g, b;

            switch (format)
            {
            case ColorStringFormat.HSLAHexOpt:
                return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, _alpha, true));

            case ColorStringFormat.HSLAPercent:
                return(HsbColorF.ToPercentParameterString(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), _alpha.ToPercentage()));

            case ColorStringFormat.HSLAValues:
                return(HsbColor32.ToValueParameterString(_hue, _saturation, _brightness, _alpha));

            case ColorStringFormat.HSLHex:
                return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, false));

            case ColorStringFormat.HSLHexOpt:
                return(HsbColor32.ToHexidecimalString(_hue, _saturation, _brightness, true));

            case ColorStringFormat.HSLPercent:
                return(HsbColorF.ToPercentParameterString(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage()));

            case ColorStringFormat.HSLValues:
                return(HsbColor32.ToValueParameterString(_hue, _saturation, _brightness));

            case ColorStringFormat.RGBAHex:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha, false));

            case ColorStringFormat.RGBAHexOpt:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha, true));

            case ColorStringFormat.RGBAPercent:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColorF.ToPercentParameterString(r, g, b, _alpha.ToPercentage()));

            case ColorStringFormat.RGBAValues:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToValueParameterString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha));

            case ColorStringFormat.RGBHex:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), false));

            case ColorStringFormat.RGBHexOpt:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToHexidecimalString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), true));

            case ColorStringFormat.RGBPercent:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColorF.ToPercentParameterString(r, g, b));

            case ColorStringFormat.RGBValues:
                ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out r, out g, out b);
                return(RgbColor32.ToValueParameterString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage()));
            }
            return(_value.ToString("X8"));
        }
Example #5
0
        /// <summary>
        /// Gets formatted string representing the current color value.
        /// </summary>
        /// <param name="format">The color string format to use.</param>
        /// <returns>The formatted string representing the current color value.</returns>
        public string ToString(ColorStringFormat format)
        {
            float h, s, b;

            switch (format)
            {
            case ColorStringFormat.HSLAHex:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha.FromPercentage(), false));

            case ColorStringFormat.HSLAHexOpt:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha.FromPercentage(), true));

            case ColorStringFormat.HSLAPercent:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColorF.ToPercentParameterString(h, s, b, _alpha));

            case ColorStringFormat.HSLAValues:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), _alpha));

            case ColorStringFormat.HSLHex:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), false));

            case ColorStringFormat.HSLHexOpt:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToHexidecimalString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), true));

            case ColorStringFormat.HSLPercent:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColorF.ToPercentParameterString(h, s, b));

            case ColorStringFormat.HSLValues:
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out h, out s, out b);
                return(HsbColor32.ToValueParameterString(h.FromDegrees(), s.FromPercentage(), b.FromPercentage()));

            case ColorStringFormat.RGBAHex:
                return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage(), false));

            case ColorStringFormat.RGBAHexOpt:
                return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage(), true));

            case ColorStringFormat.RGBAValues:
                return(RgbColor32.ToValueParameterString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), _alpha.FromPercentage()));

            case ColorStringFormat.RGBHex:
                return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), false));

            case ColorStringFormat.RGBHexOpt:
                return(RgbColor32.ToHexidecimalString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage(), true));

            case ColorStringFormat.RGBPercent:
                return(ToPercentParameterString(_red, _green, _blue));

            case ColorStringFormat.RGBValues:
                return(RgbColor32.ToValueParameterString(_red.FromPercentage(), _green.FromPercentage(), _blue.FromPercentage()));
            }
            return(ToPercentParameterString(_red, _green, _blue, _alpha));
        }
Example #6
0
        internal static bool TryParse(string text, bool strict, out RgbColor32 result)
        {
            if (text != null && (text = text.Trim()).Length > 0)
            {
                Match match = ParseRegex.Match(text);
                if (match.Success)
                {
                    try
                    {
                        if (match.Groups["h"].Success)
                        {
                            switch (text.Length)
                            {
                            case 3:
                                result = new RgbColor32(int.Parse(new string(new char[] { text[0], text[0], text[1], text[1], text[2], text[2] }), NumberStyles.HexNumber) << 8);
                                break;

                            case 4:
                                result = new RgbColor32(int.Parse(new string(new char[] { text[0], text[0], text[1], text[1], text[2], text[2] }), NumberStyles.HexNumber) << 8 | int.Parse(new string(new char[] { text[3], text[3] })));
                                break;

                            case 8:
                                result = new RgbColor32(int.Parse(text.Substring(0, 6), NumberStyles.HexNumber) << 8 | int.Parse(text.Substring(6), NumberStyles.HexNumber));
                                break;

                            default:
                                result = new RgbColor32(int.Parse(text, NumberStyles.HexNumber) << 8);
                                break;
                            }
                            return(true);
                        }

                        float alpha = 100f;
                        if (!match.Groups["a"].Success || (((match.Groups["a"].Value.EndsWith("%")) ? (float.TryParse(match.Groups["a"].Value.Substring(0, match.Groups["a"].Length - 1), out alpha) && (alpha = alpha / 100f) <= 1f) : (float.TryParse(match.Groups["a"].Value, out alpha) && alpha <= 1f)) && alpha >= 0f))
                        {
                            if (match.Groups["v"].Success)
                            {
                                int r, g, b;
                                if (int.TryParse(match.Groups["r"].Value, out r) && r > -1 && r <256 && int.TryParse(match.Groups["g"].Value, out g) && g> -1 && g <256 &&
                                                                                                                                                                    int.TryParse(match.Groups["b"].Value, out b) && b> -1 && b < 256)
                                {
                                    result = new RgbColor32((byte)r, (byte)g, (byte)b, alpha.FromPercentage());
                                    return(true);
                                }
                            }
                            else if (float.TryParse(match.Groups["r"].Value, out float rF) && rF >= 0f && rF <= 100f &&
                                     float.TryParse(match.Groups["g"].Value, out float gF) && gF >= 0f && gF <= 100f &&
                                     float.TryParse(match.Groups["b"].Value, out float bF) && bF >= 0f && bF <= 100f)
                            {
                                result = new RgbColor32((rF / 100f).FromPercentage(), (gF / 100f).FromPercentage(), (bF / 100f).FromPercentage(), alpha.FromPercentage());
                                return(true);
                            }
                        }
                    }
                    catch { }
                }
                else if (!strict && HsbColor32.TryParse(text, true, out HsbColor32 hsb))
                {
                    result = hsb.AsRgb32();
                    return(true);
                }
            }

            result = default(RgbColor32);
            return(false);
        }