Example #1
0
        public static SideBorder Parse(String str)
        {
            if (str == null)
            {
                return(SideBorder.Empty);
            }

            // The properties of a border that can be set, are (in order): border-width, border-style, and border-color.
            // It does not matter if one of the values above are missing, e.g. border:solid #ff0000; is allowed.
            // The main problem for parsing this attribute is that the browsers allow any permutation of the values... meaning more coding :(
            // http://www.w3schools.com/cssref/pr_border.asp

            List <String> borderParts = new List <String>(str.Split(HttpUtility.WhiteSpaces, StringSplitOptions.RemoveEmptyEntries));

            if (borderParts.Count == 0)
            {
                return(SideBorder.Empty);
            }

            // Initialize default values
            Unit      borderWidth = Unit.Empty;
            HtmlColor borderColor = HtmlColor.Empty;

            w.BorderValues borderStyle = w.BorderValues.Nil;

            // Now try to guess the values with their permutation

            // handle border style
            for (int i = 0; i < borderParts.Count; i++)
            {
                borderStyle = Converter.ToBorderStyle(borderParts[i]);
                if (borderStyle != w.BorderValues.Nil)
                {
                    borderParts.RemoveAt(i);                     // no need to process this part anymore
                    break;
                }
            }

            for (int i = 0; i < borderParts.Count; i++)
            {
                borderWidth = ParseWidth(borderParts[i]);
                if (borderWidth.IsValid)
                {
                    borderParts.RemoveAt(i);                     // no need to process this part anymore
                    break;
                }
            }

            // find width
            if (borderParts.Count > 0)
            {
                borderColor = HtmlColor.Parse(borderParts[0]);
            }

            // returns the instance with default value if needed.
            // These value are the ones used by the browser, i.e: solid 3px black
            return(new SideBorder(
                       borderStyle == w.BorderValues.Nil? w.BorderValues.Single : borderStyle,
                       borderColor.IsEmpty? HtmlColor.Black : borderColor,
                       borderWidth.IsFixed? borderWidth : new Unit(UnitMetric.Pixel, 4)));
        }
Example #2
0
 /// <summary>
 /// Tests whether the specified object is a HtmlColor structure and is equivalent to this color structure.
 /// </summary>
 public bool Equals(HtmlColor color)
 {
     return(color.A == A && color.R == R && color.G == G && color.B == B);
 }
Example #3
0
 public SideBorder(w.BorderValues style, HtmlColor color, Unit size)
 {
     this.style = style;
     this.color = color;
     this.size  = size;
 }
Example #4
0
        /// <summary>
        /// Convert a color using the HSL to RGB.
        /// </summary>
        /// <param name="alpha">The alpha component (0.0-1.0).</param>
        /// <param name="hue">The Hue component (0.0 - 360.0).</param>
        /// <param name="saturation">The saturation component (0.0 - 1.0).</param>
        /// <param name="luminosity">The luminosity component (0.0 - 1.0).</param>
        public static HtmlColor FromHsl(double alpha, double hue, double saturation, double luminosity)
        {
            if (alpha < 0.0 || alpha > 1.0)
            {
                throw new ArgumentOutOfRangeException(nameof(alpha), alpha, "Alpha should be comprised between 0.0 and 1.0");
            }

            if (hue < 0 || hue > 360)
            {
                throw new ArgumentOutOfRangeException(nameof(hue), hue, "Hue should be comprised between 0° and 360°");
            }

            if (saturation < 0 || saturation > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(saturation), saturation, "Saturation should be comprised between 0.0 and 1.0");
            }

            if (luminosity < 0 || luminosity > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(luminosity), luminosity, "Brightness should be comprised between 0.0 and 1.0");
            }

            if (0 == saturation)
            {
                return(HtmlColor.FromArgb(alpha, Convert.ToByte(luminosity * 255),
                                          Convert.ToByte(luminosity * 255), Convert.ToByte(luminosity * 255)));
            }

            double fMax, fMid, fMin;
            int    iSextant;

            if (0.5 < luminosity)
            {
                fMax = luminosity - (luminosity * saturation) + saturation;
                fMin = luminosity + (luminosity * saturation) - saturation;
            }
            else
            {
                fMax = luminosity + (luminosity * saturation);
                fMin = luminosity - (luminosity * saturation);
            }

            iSextant = (int)Math.Floor(hue / 60f);
            if (300f <= hue)
            {
                hue -= 360f;
            }
            hue /= 60f;
            hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
            if (0 == iSextant % 2)
            {
                fMid = hue * (fMax - fMin) + fMin;
            }
            else
            {
                fMid = fMin - hue * (fMax - fMin);
            }

            byte iMax = Convert.ToByte(fMax * 255);
            byte iMid = Convert.ToByte(fMid * 255);
            byte iMin = Convert.ToByte(fMin * 255);

            switch (iSextant)
            {
            case 1:
                return(HtmlColor.FromArgb(alpha, iMid, iMax, iMin));

            case 2:
                return(HtmlColor.FromArgb(alpha, iMin, iMax, iMid));

            case 3:
                return(HtmlColor.FromArgb(alpha, iMin, iMid, iMax));

            case 4:
                return(HtmlColor.FromArgb(alpha, iMid, iMin, iMax));

            case 5:
                return(HtmlColor.FromArgb(alpha, iMax, iMin, iMid));

            default:
                return(HtmlColor.FromArgb(alpha, iMax, iMid, iMin));
            }
        }