/// <summary>
        /// Indicates whether the current value is equal to another color model object.
        /// </summary>
        /// <param name="other">A color model to compare with this value.</param>
        /// <param name="exact"><c>true</c> to compare exact values; otherwise, <c>false</c> to compare normalized values.</param>
        /// <returns><c>true</c> if the current value is equal to another color model object; otherwise, <c>false</c>.</returns>
        public bool Equals(IRgbColorModel <float> other, bool exact)
        {
            if (other == null)
            {
                return(false);
            }
            float b;

            if (exact)
            {
                if (other.Alpha != _alpha.ToPercentage())
                {
                    return(false);
                }
                ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b);
                return(_hue.ToDegrees() == h && _saturation.ToDegrees() == s && _brightness.ToDegrees() == b);
            }

            if (other.Alpha.FromPercentage() != _alpha)
            {
                return(false);
            }

            ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out float r, out float g, out b);
            return(other.Red == r.FromPercentage() && other.Green == g.FromPercentage() && other.Blue == b.FromPercentage());
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColor32Normalized(IHsbColorModel <float> value)
 {
     if (value == null)
     {
         throw new ArgumentNullException();
     }
     _value = 0;
     if (value.Hue < 0f || value.Hue > ColorExtensions.HUE_MAXVALUE)
     {
         throw new ArgumentOutOfRangeException("hue");
     }
     if (value.Saturation < 0f || value.Saturation > 1f)
     {
         throw new ArgumentOutOfRangeException("saturation");
     }
     if (value.Brightness < 0f || value.Brightness > 1f)
     {
         throw new ArgumentOutOfRangeException("brightness");
     }
     if (value.Alpha < 0f || value.Alpha > 1f)
     {
         throw new ArgumentOutOfRangeException("alpha");
     }
     ColorExtensions.HSBtoRGB(value.Hue, value.Saturation, value.Brightness, 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.FromPercentage();;
 }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public HsbColorFNormalized MergeAverage(IEnumerable <IHsbColorModel <float> > other)
        {
            if (other == null)
            {
                return(this);
            }

            ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float rF, out float gF, out float bF);
            double r = rF, g = gF, b = bF, a = _alpha;
            double brightness = _brightness;
            int    count      = 0;

            foreach (IHsbColorModel <float> item in other)
            {
                if (item != null)
                {
                    count++;
                    ColorExtensions.HSBtoRGB(item.Hue, item.Saturation, item.Brightness, out rF, out gF, out bF);
                    r          += (double)rF;
                    g          += (double)gF;
                    b          += (double)bF;
                    a          += (double)item.Alpha;
                    brightness += (double)item.Brightness;
                }
            }
            if (count == 0)
            {
                return(this);
            }

            ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out float h, out float s, out bF);
            return(new HsbColorFNormalized(h, s, bF, (float)(a / (double)count)));
        }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="hue"></param>
 /// <param name="saturation"></param>
 /// <param name="brightness"></param>
 /// <param name="alpha"></param>
 public HsbColorFNormalized(float hue, float saturation, float brightness, float alpha)
 {
     if (hue < 0f || hue > ColorExtensions.HUE_MAXVALUE)
     {
         throw new ArgumentOutOfRangeException("hue");
     }
     if (saturation < 0f || saturation > 1f)
     {
         throw new ArgumentOutOfRangeException("saturation");
     }
     if (brightness < 0f || brightness > 1f)
     {
         throw new ArgumentOutOfRangeException("brightness");
     }
     if (alpha < 0f || alpha > 1f)
     {
         throw new ArgumentOutOfRangeException("alpha");
     }
     ColorExtensions.HSBtoRGB((hue == ColorExtensions.HUE_MAXVALUE) ? 0f : hue, saturation, brightness, out float r, out float g, out float b);
     ColorExtensions.RGBtoHSB(r, g, b, out hue, out saturation, out brightness);
     _hue        = hue;
     _saturation = saturation;
     _brightness = brightness;
     _alpha      = alpha;
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public HsbColor32Normalized MergeAverage(IEnumerable <IHsbColorModel <byte> > other)
        {
            if (other == null)
            {
                return(this);
            }

            ColorExtensions.HSBtoRGB(_hue.ToDegrees(), _saturation.ToPercentage(), _brightness.ToPercentage(), out float rF, out float gF, out float bF);
            double r = rF, g = gF, b = bF, a = _alpha.ToPercentage();
            double brightness = _brightness.ToPercentage();
            int    count      = 0;

            foreach (IHsbColorModel <byte> item in other)
            {
                if (item != null)
                {
                    count++;
                    ColorExtensions.HSBtoRGB(item.Hue.ToPercentage(), item.Saturation.ToPercentage(), item.Brightness.ToPercentage(), out rF, out gF, out bF);
                    r          += (double)rF;
                    g          += (double)gF;
                    b          += (double)bF;
                    a          += (double)item.Alpha.ToPercentage();
                    brightness += (double)item.Brightness.ToPercentage();
                }
            }
            if (count == 0)
            {
                return(this);
            }

            ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out float h, out float s, out bF);
            return(new HsbColor32Normalized(h.FromDegrees(), s.FromPercentage(), bF.FromPercentage(), ((float)(a / (double)count)).FromPercentage()));
        }
Ejemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColorFNormalized(IHsbColorModel <byte> value)
 {
     if (value == null)
     {
         throw new ArgumentNullException();
     }
     if (value.Hue < 0f || value.Hue > ColorExtensions.HUE_MAXVALUE)
     {
         throw new ArgumentOutOfRangeException("hue");
     }
     if (value.Saturation < 0f || value.Saturation > 1f)
     {
         throw new ArgumentOutOfRangeException("saturation");
     }
     if (value.Brightness < 0f || value.Brightness > 1f)
     {
         throw new ArgumentOutOfRangeException("brightness");
     }
     if (value.Alpha < 0f || value.Alpha > 1f)
     {
         throw new ArgumentOutOfRangeException("alpha");
     }
     ColorExtensions.HSBtoRGB((value.Hue == ColorExtensions.HUE_MAXVALUE) ? 0f : value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b);
     ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out float brightness);
     _hue        = hue;
     _saturation = saturation;
     _brightness = brightness;
     _alpha      = value.Alpha;
 }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <param name="exact"></param>
        /// <returns></returns>
        public bool Equals(IHsbColorModel <byte> other, bool exact)
        {
            if (other == null)
            {
                return(false);
            }
            float b;

            if (exact)
            {
                if (other.Alpha.ToPercentage() != _alpha)
                {
                    return(false);
                }
                ColorExtensions.HSBtoRGB(other.Hue.ToDegrees(), other.Saturation.ToPercentage(), other.Brightness.ToPercentage(), out float r, out float g, out b);
                return(_red == r && _green == g && _blue == b);
            }

            if (other.Alpha != _alpha.FromPercentage())
            {
                return(false);
            }

            if (!other.IsNormalized)
            {
                other = other.AsNormalized();
            }

            ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out b);
            return(other.Hue == h.FromDegrees() && other.Saturation == s.FromPercentage() && other.Brightness == b.FromPercentage());
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColor32Normalized(RgbColor32 value)
 {
     ColorExtensions.RGBtoHSB(value.Red.ToPercentage(), value.Green.ToPercentage(), value.Blue.ToPercentage(), out float h, out float s, out float b);
     _hue        = h.FromDegrees();
     _saturation = s.FromPercentage();
     _brightness = b.FromPercentage();
     _alpha      = value.Alpha;
 }
Ejemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColorFNormalized(RgbColorF value)
 {
     ColorExtensions.RGBtoHSB(value.Red, value.Green, value.Blue, out float h, out float s, out float b);
     _hue        = h;
     _saturation = s;
     _brightness = b;
     _alpha      = value.Alpha;
 }
Ejemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="value"></param>
 public HsbColorFNormalized(HsbColorF value)
 {
     ColorExtensions.HSBtoRGB(value.Hue, value.Saturation, value.Brightness, out float r, out float g, out float b);
     ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out b);
     _hue        = hue;
     _saturation = saturation;
     _brightness = b;
     _alpha      = value.Alpha;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="hue"></param>
 /// <param name="saturation"></param>
 /// <param name="brightness"></param>
 /// <param name="alpha"></param>
 public HsbColor32Normalized(byte hue, byte saturation, byte brightness, byte alpha)
 {
     _value = 0;
     ColorExtensions.HSBtoRGB(hue.ToDegrees(), saturation.ToPercentage(), 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      = alpha;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="ahsb"></param>
 public HsbColor32Normalized(int ahsb)
 {
     byte[] values = BitConverter.GetBytes(ahsb);
     ColorExtensions.HSBtoRGB(values[2].ToDegrees(), values[1].ToPercentage(), values[0].ToPercentage(), out float r, out float g, out float b);
     ColorExtensions.RGBtoHSB(r, g, b, out float hue, out float saturation, out float brightness);
     _hue        = hue.FromDegrees();
     _saturation = saturation.FromPercentage();
     _brightness = brightness.FromPercentage();
     _alpha      = values[3];
 }
 /// <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;
 }
        /// <summary>
        /// Indicates whether the current value is equal to another color model object.
        /// </summary>
        /// <param name="other">A color model to compare with this value.</param>
        /// <param name="exact"><c>true</c> to compare exact values; otherwise, <c>false</c> to compare normalized values.</param>
        /// <returns><c>true</c> if the current value is equal to another color model object; otherwise, <c>false</c>.</returns>
        public bool Equals(IRgbColorModel <byte> other, bool exact)
        {
            if (other == null || _alpha != other.Alpha)
            {
                return(false);
            }
            float b;

            if (exact)
            {
                ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b);
                return(_hue == h.FromDegrees() && _saturation == s.FromPercentage() && _brightness == b.FromPercentage());
            }
            ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float r, out float g, out b);
            return(other.Red == r.FromPercentage() && other.Green == g.FromPercentage() && other.Blue == b.FromPercentage());
        }
Ejemplo n.º 15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <param name="exact"></param>
        /// <returns></returns>
        public bool Equals(IRgbColorModel <float> other, bool exact)
        {
            if (other == null || _alpha != other.Alpha)
            {
                return(false);
            }

            float b;

            if (exact)
            {
                ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out b);
                return(_hue == h && _saturation == s && _brightness == b);
            }

            ColorExtensions.HSBtoRGB(_hue, _saturation, _brightness, out float r, out float g, out b);
            return(other.Red == r && other.Green == g && other.Blue == b);
        }
Ejemplo n.º 16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <param name="exact"></param>
        /// <returns></returns>
        public bool Equals(IRgbColorModel <float> other, bool exact)
        {
            if (other == null)
            {
                return(false);
            }
            if (exact)
            {
                if (other.Alpha != _alpha.ToPercentage())
                {
                    return(false);
                }
                ColorExtensions.RGBtoHSB(other.Red, other.Green, other.Blue, out float h, out float s, out float b);
                return(_hue.ToDegrees() == h && _saturation.ToDegrees() == s && _brightness.ToDegrees() == b);
            }

            return(AsNormalized().Equals(other, false));
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Returns a <see cref="RgbColorF" /> value with the color brightness adjusted.
 /// </summary>
 /// <param name="percentage">The percentage to saturate the color, ranging from -1.0 to 1.0. A positive value increases brightness, a negative value decreases brightness and a zero vale has no effect.</param>
 /// <returns>A <see cref="RgbColorF" /> value with the color brightness adjusted.</returns>
 /// <remarks>For positive values, the target brightness value is determined using the following formula: <c>brightness + (1.0 - brightness) * percentage</c>
 /// <para>For negative values, the target brightness value is determined using the following formula: <c>brightness + brightness * percentage</c></para></remarks>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="percentage" /> is less than -1.0 or <paramref name="percentage" /> is greater than 1.0.</exception>
 public RgbColorF ShiftBrightness(float percentage)
 {
     if (percentage < -1f || percentage > 1f)
     {
         throw new ArgumentOutOfRangeException("percentage");
     }
     if (percentage == 0f)
     {
         return(this);
     }
     ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float b);
     if ((percentage == 1f) ? b == 1f : percentage == -1f && b == 0f)
     {
         return(this);
     }
     ColorExtensions.HSBtoRGB(h, s, b + ((percentage > 0f) ? (1f - b) : b) * percentage, out float r, out float g, out b);
     return(new RgbColorF(r, g, b, _alpha));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Returns a <see cref="RgbColor32" /> value with the color saturation adjusted.
 /// </summary>
 /// <param name="percentage">The percentage to saturate the color, ranging from -1.0 to 1.0. A positive value increases saturation, a negative value decreases saturation and a zero vale has no effect.</param>
 /// <returns>A <see cref="RgbColor32" /> value with the color saturation adjusted.</returns>
 /// <remarks>For positive values, the target saturation value is determined using the following formula: <c>saturation + (MAX_VALUE - saturation) * percentage</c>
 /// <para>For negative values, the target saturation value is determined using the following formula: <c>saturation + saturation * percentage</c></para></remarks>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="percentage" /> is less than -1.0 or <paramref name="percentage" /> is greater than 1.0.</exception>
 public RgbColor32 ShiftSaturation(float percentage)
 {
     if (percentage < -1f || percentage > 1f)
     {
         throw new ArgumentOutOfRangeException("percentage");
     }
     if (percentage == 0f)
     {
         return(this);
     }
     ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out float h, out float s, out float b);
     if ((percentage == 1f) ? s == 1f : percentage == -1f && s == 0f)
     {
         return(this);
     }
     ColorExtensions.HSBtoRGB(h, s + ((percentage > 0f) ? (1f - s) : s) * percentage, b, out float r, out float g, out b);
     return(new RgbColor32(r.FromPercentage(), g.FromPercentage(), b.FromPercentage(), _alpha));
 }
Ejemplo n.º 19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <param name="exact"></param>
        /// <returns></returns>
        public bool Equals(IRgbColorModel <byte> other, bool exact)
        {
            if (other == null)
            {
                return(false);
            }

            float b;

            if (exact)
            {
                if (other.Alpha.ToPercentage() != _alpha)
                {
                    return(false);
                }
                ColorExtensions.RGBtoHSB(other.Red.ToPercentage(), other.Green.ToPercentage(), other.Blue.ToPercentage(), out float h, out float s, out b);
                return(_hue == h && _saturation == s && _brightness == b);
            }

            return(AsNormalized().Equals(other, false));
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Returns a <see cref="RgbColorF" /> value with the color hue adjusted.
 /// </summary>
 /// <param name="degrees">The number of degrees to shift the hue value, ranging from -360.0 to 360.0. A positive value shifts the hue in the red-to-cyan direction, and a negative value shifts the hue in the cyan-to-red direction.</param>
 /// <returns>A <see cref="RgbColorF" /> value with the color hue adjusted.</returns>
 /// <remarks>The values 0.0, -360.0 and 360.0 have no effect since they would result in no hue change.</remarks>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="degrees" /> is less than -360.0 or <paramref name="degrees" /> is greater than 360.0.</exception>
 public RgbColorF ShiftHue(float degrees)
 {
     if (degrees < -360f || degrees > 360f)
     {
         throw new ArgumentOutOfRangeException("degrees");
     }
     if (degrees == 0f || degrees == 360f || degrees == -360f)
     {
         return(this);
     }
     ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float b);
     h += degrees;
     if (h < 0f)
     {
         h += 360f;
     }
     else if (h >= 360f)
     {
         h -= 360f;
     }
     ColorExtensions.HSBtoRGB(h, s, b, out float r, out float g, out b);
     return(new RgbColorF(r, g, b, _alpha));
 }
Ejemplo n.º 21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <param name="exact"></param>
        /// <returns></returns>
        public bool Equals(IHsbColorModel <float> other, bool exact)
        {
            if (other == null || _alpha != other.Alpha)
            {
                return(false);
            }

            float b;

            if (exact)
            {
                ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out b);
                return(other.Hue == h && other.Saturation == s && other.Brightness == b);
            }

            if (!other.IsNormalized)
            {
                other = other.AsNormalized();
            }

            ColorExtensions.HSBtoRGB(other.Hue, other.Saturation, other.Brightness, out float r, out float g, out b);
            return(_red == r && _green == g && _blue == b);
        }
Ejemplo n.º 22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public RgbColor32 MergeAverage(IEnumerable <IRgbColorModel <byte> > other)
        {
            if (other == null)
            {
                return(this);
            }

            double r = _red.ToPercentage(), g = _green.ToPercentage(), b = _blue.ToPercentage(), a = _alpha.ToPercentage();

            ColorExtensions.RGBtoHSB(_red.ToPercentage(), _green.ToPercentage(), _blue.ToPercentage(), out float h, out float s, out float bF);

            double brightness = bF;
            int    count      = 0;

            foreach (IRgbColorModel <byte> item in other)
            {
                if (item != null)
                {
                    count++;
                    r += (double)item.Red.ToPercentage();
                    g += (double)item.Green.ToPercentage();
                    b += (double)item.Blue.ToPercentage();
                    a += (double)item.Alpha.ToPercentage();
                    ColorExtensions.RGBtoHSB(item.Red.ToPercentage(), item.Green.ToPercentage(), item.Blue.ToPercentage(), out h, out s, out bF);
                    brightness += (double)bF;
                }
            }
            if (count == 0)
            {
                return(this);
            }

            ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out h, out s, out bF);
            ColorExtensions.HSBtoRGB(h, s, (float)(brightness / (double)count), out float red, out float green, out bF);
            return(new RgbColor32(red.FromPercentage(), green.FromPercentage(), bF.FromPercentage(), ((float)(a / (double)count)).FromPercentage()));
        }
Ejemplo n.º 23
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public RgbColorF MergeAverage(IEnumerable <IRgbColorModel <float> > other)
        {
            if (other == null)
            {
                return(this);
            }

            double r = _red, g = _green, b = _blue, a = _alpha;

            ColorExtensions.RGBtoHSB(_red, _green, _blue, out float h, out float s, out float bF);

            double brightness = bF;
            int    count      = 0;

            foreach (IRgbColorModel <float> item in other)
            {
                if (item != null)
                {
                    count++;
                    r += (double)item.Red;
                    g += (double)item.Green;
                    b += (double)item.Blue;
                    a += (double)item.Alpha;
                    ColorExtensions.RGBtoHSB(item.Red, item.Green, item.Blue, out h, out s, out bF);
                    brightness += (double)bF;
                }
            }
            if (count == 0)
            {
                return(this);
            }

            ColorExtensions.RGBtoHSB((float)(r / (double)count), (float)(r / (double)count), (float)(r / (double)count), out h, out s, out bF);
            ColorExtensions.HSBtoRGB(h, s, (float)(brightness / (double)count), out float red, out float green, out bF);
            return(new RgbColorF(red, green, bF, (float)(a / (double)count)));
        }
Ejemplo n.º 24
0
        internal static bool TryParse(string text, bool strict, out HsbColor32 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 HsbColor32(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 HsbColor32(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 HsbColor32(int.Parse(text.Substring(0, 6), NumberStyles.HexNumber) << 8 | int.Parse(text.Substring(6), NumberStyles.HexNumber));
                                break;

                            default:
                                result = new HsbColor32(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 HsbColor32((byte)h, (byte)s, (byte)l, alpha.FromPercentage());
                                    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 HsbColor32(hF.FromDegrees(), (sF / 100f).FromPercentage(), (lF / 100f).FromPercentage(), alpha.FromPercentage());
                                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 HsbColor32(h.FromDegrees(), s.FromPercentage(), b.FromPercentage(), rgb.Alpha.FromPercentage());
                    return(true);
                }
            }

            result = default(HsbColor32);
            return(false);
        }
Ejemplo n.º 25
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));
        }