Ejemplo n.º 1
0
        /// <summary>
        /// Converts a color described in terms of alpha, red, green and blue channels (RGB) to 
        /// its equivalent HLS Color.
        /// </summary>
        /// <param name="rgbColor">The RGB color to convert.</param>
        /// <returns>An equivalent HLS Color.</returns>
        public static HlsColor FromRgb(Color rgbColor)
        {
            var hlsColor = new HlsColor();

            var red = rgbColor.R / 255.0;
            var green = rgbColor.G / 255.0;
            var blue = rgbColor.B / 255.0;
            var alpha = rgbColor.A / 255.0;

            var minVal = Math.Min(red, Math.Min(green, blue));
            var maxVal = Math.Max(red, Math.Max(green, blue));
            var delta = maxVal - minVal;

            if (maxVal == minVal)
            {
                hlsColor.H = 0.0;
                hlsColor.S = 0.0;
                hlsColor.L = maxVal;
                return hlsColor;
            }

            hlsColor.L = (minVal + maxVal) / 2.0;
            hlsColor.S = delta / ((hlsColor.L < 0.5) ? (maxVal + minVal) : (2.0 - maxVal - minVal));

            if (red == maxVal) hlsColor.H = (green - blue) / delta;
            if (green == maxVal) hlsColor.H = 2.0 + ((blue - red) / delta);
            if (blue == maxVal) hlsColor.H = 4.0 + ((red - green) / delta);

            hlsColor.H *= 60;
            if (hlsColor.H < 0) hlsColor.H += 360;

            hlsColor.A = alpha;

            return hlsColor;
        }
        /// <summary>
        /// Converts a color described in terms of alpha, red, green and blue channels (RGB) to
        /// its equivalent HLS Color.
        /// </summary>
        /// <param name="rgbColor">The RGB color to convert.</param>
        /// <returns>An equivalent HLS Color.</returns>
        public static HlsColor FromRgb(Color rgbColor)
        {
            var hlsColor = new HlsColor();

            var red   = rgbColor.R / 255.0;
            var green = rgbColor.G / 255.0;
            var blue  = rgbColor.B / 255.0;
            var alpha = rgbColor.A / 255.0;

            var minVal = Math.Min(red, Math.Min(green, blue));
            var maxVal = Math.Max(red, Math.Max(green, blue));
            var delta  = maxVal - minVal;

            if (maxVal == minVal)
            {
                hlsColor.H = 0.0;
                hlsColor.S = 0.0;
                hlsColor.L = maxVal;
                return(hlsColor);
            }

            hlsColor.L = (minVal + maxVal) / 2.0;
            hlsColor.S = delta / ((hlsColor.L < 0.5) ? (maxVal + minVal) : (2.0 - maxVal - minVal));

            if (red == maxVal)
            {
                hlsColor.H = (green - blue) / delta;
            }
            if (green == maxVal)
            {
                hlsColor.H = 2.0 + ((blue - red) / delta);
            }
            if (blue == maxVal)
            {
                hlsColor.H = 4.0 + ((red - green) / delta);
            }

            hlsColor.H *= 60;
            if (hlsColor.H < 0)
            {
                hlsColor.H += 360;
            }

            hlsColor.A = alpha;

            return(hlsColor);
        }
        /// <summary>
        /// Converts the color of the supplied brush changing its luminosity by the given factor.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The factor used to adjust the luminosity (0..1).</param>
        /// <param name="culture">The culture to use in the converter (unused).</param>
        /// <returns>A converted value.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
#endif
        {
            if (value == null)
            {
                return(null);
            }
            if (parameter == null)
            {
                return(null);
            }

            var factor = double.Parse(parameter.ToString(), CultureInfo.InvariantCulture);

            if (value is SolidColorBrush)
            {
                var color    = ((SolidColorBrush)value).Color;
                var hlsColor = HlsColor.FromRgb(color);
                hlsColor.L *= factor;
                return(new SolidColorBrush(hlsColor.ToRgb()));
            }
            else if (value is LinearGradientBrush)
            {
                var gradientStops = new GradientStopCollection();
                foreach (var stop in ((LinearGradientBrush)value).GradientStops)
                {
                    var hlsColor = HlsColor.FromRgb(stop.Color);
                    hlsColor.L *= factor;
                    gradientStops.Add(new GradientStop()
                    {
                        Color = hlsColor.ToRgb(), Offset = stop.Offset
                    });
                }

                var brush = new LinearGradientBrush(gradientStops, 0.0);
                return(brush);
            }

            return(value);
        }