Ejemplo n.º 1
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 r, g, b;

            switch (format)
            {
            case ColorStringFormat.HSLAHex:
                return(HsbColor32.ToHexidecimalString(_hue.FromDegrees(), _saturation.FromPercentage(), _brightness.FromPercentage(), _alpha.FromPercentage(), false));

            case ColorStringFormat.HSLAHexOpt:
                return(HsbColor32.ToHexidecimalString(_hue.FromDegrees(), _saturation.FromPercentage(), _brightness.FromPercentage(), _alpha.FromPercentage(), true));

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

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

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

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

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

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

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

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

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

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

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

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

            case ColorStringFormat.RGBValues:
                ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out r, out g, out b);
                return(RgbColor32.ToValueParameterString(r.FromPercentage(), g.FromPercentage(), b.FromPercentage()));
            }
            return(HsbColorF.ToPercentParameterString(_hue, _saturation, _brightness, _alpha));
        }
Ejemplo n.º 2
0
        internal static bool TryParse(string text, bool strict, out HsbColorF result)
        {
            if (text != null && (text = text.Trim()).Length > 0)
            {
                Match match = HsbColor32.ParseRegex.Match(text);
                if (match.Success)
                {
                    try
                    {
                        if (match.Groups["h"].Success)
                        {
                            switch (text.Length)
                            {
                            case 3:
                                result = new HsbColorF(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 HsbColorF(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 HsbColorF(int.Parse(text.Substring(0, 6), NumberStyles.HexNumber) << 8 | int.Parse(text.Substring(6), NumberStyles.HexNumber));
                                break;

                            default:
                                result = new HsbColorF(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["b"].Success)
                            {
                                int h, s, l;
                                if (int.TryParse(match.Groups["h"].Value, out h) && h > -1 && h <256 && int.TryParse(match.Groups["s"].Value, out s) && s> -1 && s <256 &&
                                                                                                                                                                    int.TryParse(match.Groups["l"].Value, out l) && l> -1 && l < 256)
                                {
                                    result = new HsbColorF(((byte)h).ToDegrees(), ((byte)s).ToPercentage(), ((byte)l).ToPercentage(), alpha);
                                    return(true);
                                }
                            }
                            else if (float.TryParse(match.Groups["h"].Value, out float hF) && hF >= 0f && hF <= 360f && float.TryParse(match.Groups["s"].Value, out float sF) && sF >= 0f && sF <= 100f && float.TryParse(match.Groups["l"].Value, out float lF) && lF >= 0f && lF <= 100f)
                            {
                                result = new HsbColorF(hF, sF / 100f, lF / 100f, alpha);
                                return(true);
                            }
                        }
                    }
                    catch { }
                }
                else if (!strict && RgbColorF.TryParse(text, true, out RgbColorF rgb))
                {
                    ColorExtensions.RGBtoHSB(rgb.Red, rgb.Blue, rgb.Green, out float h, out float s, out float b);
                    result = new HsbColorF(h, s, b, rgb.Alpha);
                    return(true);
                }
            }

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