Exemplo n.º 1
0
        /// <summary>
        /// Mixes the specified color into the current color with an optional percentage amount.
        /// </summary>
        /// <param name="mixColor">Color of the mix.</param>
        /// <param name="percentageAmount">The percentage amount.</param>
        public void Mix(RockColor mixColor, int percentageAmount = 50)
        {
            var amount = ( double )percentageAmount / ( double )100;

            R = ( double )((mixColor.R * amount) + this.R * (1 - amount));
            G = ( double )((mixColor.G * amount) + this.G * (1 - amount));
            B = ( double )((mixColor.B * amount) + this.B * (1 - amount));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RockColor" /> class.
        /// </summary>
        /// <param name="color">The color in hexidecimal or rgba format.</param>
        public RockColor(string color)
        {
            // #ee7625
            if (color.StartsWith("#"))
            {
                ParseHexString(color);
                return;
            }

            // rgba(255, 99, 71, 0.5)
            if (color.StartsWith("rgba"))
            {
                color = color.Replace(" ", "").Replace("rgba(", "").Replace(")", "");
                var parts = color.Split(',');
                if (parts.Length == 4)
                {
                    this.R     = parts[0].Trim().AsDoubleForColor();
                    this.G     = parts[1].Trim().AsDoubleForColor();
                    this.B     = parts[2].Trim().AsDoubleForColor();
                    this.Alpha = parts[3].Trim().AsDoubleForColor();
                }
            }

            // rgb(255, 99, 71)
            if (color.StartsWith("rgb"))
            {
                color = color.Replace(" ", "").Replace("rgb(", "").Replace(")", "");

                var parts = color.Split(',');
                if (parts.Length == 3)
                {
                    this.R = parts[0].Trim().AsDoubleForColor();
                    this.G = parts[1].Trim().AsDoubleForColor();
                    this.B = parts[2].Trim().AsDoubleForColor();
                }
            }

            // Othewise assume that the color is a named color 'blue'
            var namedColor = RockColor.GetColorFromKeyword(color);

            if (namedColor != null)
            {
                this.R     = namedColor.R;
                this.G     = namedColor.G;
                this.B     = namedColor.B;
                this.Alpha = namedColor.Alpha; // For the case of 'transparent'
            }

            UpdateHslFromRgb();
        }