Пример #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));
        }
Пример #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().AsDouble();
                    this.G     = parts[1].Trim().AsDouble();
                    this.B     = parts[2].Trim().AsDouble();
                    this.Alpha = parts[3].Trim().AsDouble();
                }
            }

            // 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().AsDouble();
                    this.G = parts[1].Trim().AsDouble();
                    this.B = parts[2].Trim().AsDouble();
                }
            }

            // 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();
        }