/// <inheritdoc /> public string Between(string fromSize, string toSize, string minScreen = "320px", string maxScreen = "1200px") { ValueAndUnit fromSizeValueAndUnit = _helpers.GetValueAndUnit(fromSize); ValueAndUnit toSizeValueAndUnit = _helpers.GetValueAndUnit(toSize); ValueAndUnit minScreenValueAndUnit = _helpers.GetValueAndUnit(minScreen); ValueAndUnit maxScreenValueAndUnit = _helpers.GetValueAndUnit(maxScreen); if (!double.TryParse(fromSizeValueAndUnit.Value, out double fromSizeNum) || !double.TryParse(toSizeValueAndUnit.Value, out double toSizeNum) || string.IsNullOrWhiteSpace(fromSizeValueAndUnit.Unit) || string.IsNullOrWhiteSpace(toSizeValueAndUnit.Unit) || fromSizeValueAndUnit.Unit != toSizeValueAndUnit.Unit) { throw PolishedException.GetPolishedException(47); } if (!double.TryParse(minScreenValueAndUnit.Value, out double minScreenNum) || !double.TryParse(maxScreenValueAndUnit.Value, out double maxScreenNum) || string.IsNullOrWhiteSpace(minScreenValueAndUnit.Unit) || string.IsNullOrWhiteSpace(maxScreenValueAndUnit.Unit) || minScreenValueAndUnit.Unit != maxScreenValueAndUnit.Unit) { throw PolishedException.GetPolishedException(48); } double slope = (fromSizeNum - toSizeNum) / (minScreenNum - maxScreenNum); double baseNum = toSizeNum - slope * maxScreenNum; return($"calc({Math.Round(baseNum, 2)}{fromSizeValueAndUnit.Unit} + {Math.Round(100 * slope, 2)}vw)"); }
/// <inheritdoc /> public string FluidRange(List <FluidRangeConfiguration> cssProps, string minScreen = "320px", string maxScreen = "1200px") { StringBuilder fallbacks = new StringBuilder(); StringBuilder minSb = new StringBuilder(); StringBuilder maxSb = new StringBuilder(); minSb.Append("@media(min-width:").Append(minScreen).Append("){"); maxSb.Append("@media(min-width:").Append(maxScreen).Append("){"); if (cssProps == null || cssProps.Count == 0) { throw PolishedException.GetPolishedException(49); } foreach (FluidRangeConfiguration cssProp in cssProps) { if (string.IsNullOrWhiteSpace(cssProp.Prop) || string.IsNullOrWhiteSpace(cssProp.FromSize) || string.IsNullOrWhiteSpace(cssProp.ToSize)) { throw PolishedException.GetPolishedException(50); } string between = Between(cssProp.FromSize, cssProp.ToSize, minScreen, maxScreen); fallbacks.Append($"{cssProp.Prop}:{cssProp.FromSize};"); minSb.Append(cssProp.Prop).Append(":").Append(between).Append(";"); maxSb.Append(cssProp.Prop).Append(":").Append(cssProp.ToSize).Append(";"); } minSb.Append("}"); maxSb.Append("}"); return(fallbacks.ToString() + minSb.ToString() + maxSb.ToString()); }
/// <inheritdoc /> public string Animation(params string[] args) { if (args.Count() > 8) { throw PolishedException.GetPolishedException(64); } return("animation: " + string.Join(", ", args) + ";"); }
/// <inheritdoc /> public string Animation(params string[][] args) { if (args.Max(arg => arg.Count()) > 8) { throw PolishedException.GetPolishedException(66); } List <string> ret = args.Select(arg => string.Join(" ", arg)).ToList(); return("animation: " + string.Join(", ", ret) + ";"); }
/// <inheritdoc /> public string RetinaImage(string filename, string backgroundSize, string extension = "png", string retinaFilename = "", string retinaSuffix = "_2x") { if (string.IsNullOrWhiteSpace(filename)) { throw PolishedException.GetPolishedException(58); } string ext = string.IsNullOrWhiteSpace(extension) ? "png" : extension.Replace(".", ""); string rFilename = !string.IsNullOrWhiteSpace(retinaFilename) ? $"{retinaFilename}.{ext}" : $"{filename}{retinaSuffix}.{ext}"; string rBackgroundSize = !string.IsNullOrWhiteSpace(backgroundSize) ? $"background-size:{backgroundSize};" : ""; return($"background-image:url({filename}.{ext});{HiDPI()}{{background-image:url({rFilename});{rBackgroundSize}}}"); }
/// <inheritdoc /> public string ModularScale(int steps, string baseNum = "1em", ModularScaleRatio modularScaleRatio = ModularScaleRatio.PerfectFourth) { string unit = GetUnit(baseNum); if (!double.TryParse(StripUnit(baseNum), out double realBase)) { throw PolishedException.GetPolishedException(44, baseNum); } double ratio = _ratioNames[modularScaleRatio]; double ret = realBase * Math.Pow(ratio, steps); return($"{ret}{unit}"); }
/// <inheritdoc /> public string FluidRange(FluidRangeConfiguration cssProp, string minScreen = "320px", string maxScreen = "1200px") { if (cssProp == null) { throw PolishedException.GetPolishedException(49); } if (string.IsNullOrWhiteSpace(cssProp.Prop) || string.IsNullOrWhiteSpace(cssProp.FromSize) || string.IsNullOrWhiteSpace(cssProp.ToSize)) { throw PolishedException.GetPolishedException(51); } string between = Between(cssProp.FromSize, cssProp.ToSize, minScreen, maxScreen); return($"{cssProp.Prop}:{cssProp.FromSize};@media(min-width:{minScreen}){{{cssProp.Prop}:{between};}}@media(min-width:{maxScreen}){{{cssProp.Prop}:{cssProp.ToSize};}}"); }
/// <inheritdoc /> public string RadialGradient(List <string> colorStops, string extent, string fallback, string position, string shape) { if (colorStops == null || colorStops.Count < 2) { throw PolishedException.GetPolishedException(57); } StringBuilder sb = new StringBuilder(); sb.Append("background-color:").Append(fallback ?? colorStops[0].Split(' ')[0]).Append(";"); sb.Append("background-image:").Append("radial-gradient("); sb.Append(RadialGradientFirstValue(position, shape, extent)); sb.Append(string.Join(", ", colorStops)); sb.Append(");"); return(sb.ToString()); }
/// <inheritdoc /> public string Triangle(Side pointingDirection, string height, string width, string foregroundColor, string backgroundColor = "transparent") { ValueAndUnit heightAndUnit = _helpers.GetValueAndUnit(height); ValueAndUnit widthAndUnit = _helpers.GetValueAndUnit(width); if (!double.TryParse(heightAndUnit.Value, out double h) || !double.TryParse(widthAndUnit.Value, out double w)) { throw PolishedException.GetPolishedException(60); } string backgourndColorDefault = !string.IsNullOrWhiteSpace(backgroundColor) ? backgroundColor : "transparent"; StringBuilder sb = new StringBuilder(); sb.Append("width:0;"); sb.Append("height:0;"); sb.Append("border-color:").Append(GetBorderColor(pointingDirection, foregroundColor, backgourndColorDefault)).Append(";"); sb.Append("border-style:solid;"); sb.Append("border-width:").Append(GetBorderWidth(pointingDirection, heightAndUnit, widthAndUnit)).Append(";"); return(sb.ToString()); }
/// <inheritdoc /> public string LinearGradient(List <string> colorStops, string fallback, string toDirection) { if (colorStops == null || colorStops.Count < 2) { throw PolishedException.GetPolishedException(56); } StringBuilder sb = new StringBuilder(); sb.Append("background-color:").Append(fallback ?? colorStops[0].Split(' ')[0]).Append(";"); sb.Append("background-image:").Append("linear-gradient("); if (!string.IsNullOrWhiteSpace(toDirection)) { sb.Append(toDirection).Append(", "); } sb.Append(string.Join(", ", colorStops)); sb.Append(");"); return(sb.ToString()); }
/// <summary> /// Returns an HslColor object from a string. /// </summary> /// <param name="color"></param> /// <returns></returns> public static RgbColor Parse(string color) { string hexRegex = "^#[a-fA-F0-9]{6}$"; string hexRgbaRegex = "^#[a-fA-F0-9]{8}$"; string reducedHexRegex = "^#[a-fA-F0-9]{3}$"; string reducedRgbaHexRegex = "^#[a-fA-F0-9]{4}$"; string rgbRegex = @"rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$"; string rgbaRegex = @"^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([-+]?[0-9]*[.]?[0-9]+)\s*\)$"; string hslRegex = @"^hsl\(\s*(\d{0,3}[.]?[0-9]+)\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$"; string hslaRegex = @"^hsla\(\s*(\d{0,3}[.]?[0-9]+)\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*([-+]?[0-9]*[.]?[0-9]+)\s*\)$"; Conversion conversion = new Conversion(); string normalizedColor = conversion.NameToHex(color); if (Regex.IsMatch(normalizedColor, hexRegex)) { return(new RgbColor { Red = int.Parse($"{normalizedColor[1]}{normalizedColor[2]}", System.Globalization.NumberStyles.HexNumber), Green = int.Parse($"{normalizedColor[3]}{normalizedColor[4]}", System.Globalization.NumberStyles.HexNumber), Blue = int.Parse($"{normalizedColor[5]}{normalizedColor[6]}", System.Globalization.NumberStyles.HexNumber) }); } if (Regex.IsMatch(normalizedColor, hexRgbaRegex)) { double alpha = Math.Round( ((double)int.Parse($"{normalizedColor[7]}{normalizedColor[8]}", System.Globalization.NumberStyles.HexNumber) / 255) , 2); return(new RgbColor { Red = int.Parse($"{normalizedColor[1]}{normalizedColor[2]}", System.Globalization.NumberStyles.HexNumber), Green = int.Parse($"{normalizedColor[3]}{normalizedColor[4]}", System.Globalization.NumberStyles.HexNumber), Blue = int.Parse($"{normalizedColor[5]}{normalizedColor[6]}", System.Globalization.NumberStyles.HexNumber), Alpha = alpha }); } if (Regex.IsMatch(normalizedColor, reducedHexRegex)) { return(new RgbColor { Red = int.Parse($"{normalizedColor[1]}{normalizedColor[1]}", System.Globalization.NumberStyles.HexNumber), Green = int.Parse($"{normalizedColor[2]}{normalizedColor[2]}", System.Globalization.NumberStyles.HexNumber), Blue = int.Parse($"{normalizedColor[3]}{normalizedColor[3]}", System.Globalization.NumberStyles.HexNumber) }); } if (Regex.IsMatch(normalizedColor, reducedRgbaHexRegex)) { double alpha = Math.Round( ((double)int.Parse($"{normalizedColor[4]}{normalizedColor[4]}", System.Globalization.NumberStyles.HexNumber) / 255) , 2); return(new RgbColor { Red = int.Parse($"{normalizedColor[1]}{normalizedColor[1]}", System.Globalization.NumberStyles.HexNumber), Green = int.Parse($"{normalizedColor[2]}{normalizedColor[2]}", System.Globalization.NumberStyles.HexNumber), Blue = int.Parse($"{normalizedColor[3]}{normalizedColor[3]}", System.Globalization.NumberStyles.HexNumber), Alpha = alpha }); } MatchCollection rgbMatched = new Regex(rgbRegex, RegexOptions.IgnoreCase).Matches(normalizedColor); if (rgbMatched.Count > 0) { return(new RgbColor { Red = int.Parse(rgbMatched[0].Groups[1].Value), Green = int.Parse(rgbMatched[0].Groups[2].Value), Blue = int.Parse(rgbMatched[0].Groups[3].Value) }); } MatchCollection rgbaMatched = new Regex(rgbaRegex, RegexOptions.IgnoreCase).Matches(normalizedColor); if (rgbaMatched.Count > 0) { return(new RgbColor { Red = int.Parse(rgbaMatched[0].Groups[1].Value), Green = int.Parse(rgbaMatched[0].Groups[2].Value), Blue = int.Parse(rgbaMatched[0].Groups[3].Value), Alpha = double.Parse(rgbaMatched[0].Groups[4].Value) }); } MatchCollection hslMatched = new Regex(hslRegex, RegexOptions.IgnoreCase).Matches(normalizedColor); if (hslMatched.Count > 0) { double hue = double.Parse(hslMatched[0].Groups[1].Value); double saturation = double.Parse(hslMatched[0].Groups[2].Value) / 100; double lightness = double.Parse(hslMatched[0].Groups[3].Value) / 100; HslColor hsl = new HslColor { Hue = hue, Saturation = saturation, Lightness = lightness }; return(hsl.ToRgb()); } MatchCollection hslaMatched = new Regex(hslaRegex, RegexOptions.IgnoreCase).Matches(normalizedColor); if (hslaMatched.Count > 0) { double hue = double.Parse(hslaMatched[0].Groups[1].Value); double saturation = double.Parse(hslaMatched[0].Groups[2].Value) / 100; double lightness = double.Parse(hslaMatched[0].Groups[3].Value) / 100; HslColor hsl = new HslColor { Hue = hue, Saturation = saturation, Lightness = lightness }; RgbColor rgba = hsl.ToRgb(); rgba.Alpha = double.Parse(hslaMatched[0].Groups[4].Value); return(rgba); } throw PolishedException.GetPolishedException(5); }
/// <inheritdoc /> public string FontFace(string fontFamily, string fontFilePath, string fontStretch, string fontStyle, string fontVariant, string fontWeight, List <string> fileFormats, bool formatHint, List <string> localFonts, string unicodeRange, string fontDisplay, string fontVariationSettings, string fontFeatureSettings) { if (string.IsNullOrWhiteSpace(fontFamily)) { throw PolishedException.GetPolishedException(55); } if (fileFormats == null || fileFormats.Count == 0) { fileFormats = new List <string> { "eot", "woff2", "woff", "ttf", "svg" }; } if (string.IsNullOrWhiteSpace(fontFilePath) && localFonts == null) { throw PolishedException.GetPolishedException(52); } if (localFonts != null && localFonts.Count == 0) { throw PolishedException.GetPolishedException(53); } StringBuilder sb = new StringBuilder(); sb.Append("@font-face{"); sb.Append("font-family:").Append(fontFamily).Append(";"); sb.Append("src:").Append(GenerateSources(fontFilePath, localFonts, fileFormats, formatHint)).Append(";"); if (unicodeRange != null) { sb.Append("unicode-range:").Append(unicodeRange).Append(";"); } if (fontStretch != null) { sb.Append("font-stretch:").Append(fontStretch).Append(";"); } if (fontStyle != null) { sb.Append("font-style:").Append(fontStyle).Append(";"); } if (fontVariant != null) { sb.Append("font-variant:").Append(fontVariant).Append(";"); } if (fontWeight != null) { sb.Append("font-weight:").Append(fontWeight).Append(";"); } if (fontDisplay != null) { sb.Append("font-display:").Append(fontDisplay).Append(";"); } if (fontVariant != null) { sb.Append("font-variant:").Append(fontVariant).Append(";"); } if (fontVariationSettings != null) { sb.Append("font-variant-settings:").Append(fontVariationSettings).Append(";"); } if (fontFeatureSettings != null) { sb.Append("font-feature-settings:").Append(fontFeatureSettings).Append(";"); } sb.Append("}"); return(sb.ToString()); }