Advertisement
enevlogiev

hsl

Jun 16th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace WorkWave.CustomerPortal.Services.Utils
  6. {
  7.     public class ScssCompiler
  8.     {
  9.         private Dictionary<string, string> scssVariables = new Dictionary<string, string>();
  10.  
  11.         public string Compile(string scss)
  12.         {
  13.             scss = this.GetSassVariables(scss);
  14.  
  15.             scss = this.ReplaceSassVariables(scss);
  16.  
  17.             scss = this.ApplyDarkenFunction(scss);
  18.  
  19.             var a = 5;
  20.  
  21.             return "asd";
  22.         }
  23.  
  24.         private string ReplaceSassVariables(string scss)
  25.         {
  26.             foreach (var pair in scssVariables)
  27.             {
  28.                 scss = scss.Replace(pair.Key, pair.Value);
  29.             }
  30.  
  31.             return scss;
  32.         }
  33.  
  34.         private string GetSassVariables(string scss)
  35.         {
  36.             var variableRegex = new Regex(@"\$[a-z\-]+:#[\w\d]+;");
  37.             var matches = variableRegex.Matches(scss);
  38.  
  39.             foreach (Match match in matches)
  40.             {
  41.                 var args = match.Value.Split(':');
  42.                 var variableName = args[0];
  43.                 var variableValue = args[1].Trim(';');
  44.                 scssVariables[variableName] = variableValue;
  45.  
  46.                 scss = scss.Replace(match.Value, string.Empty);
  47.             }
  48.  
  49.             return scss;
  50.         }
  51.  
  52.         private string ApplyDarkenFunction(string scss)
  53.         {
  54.             var darkenRegex = new Regex(@"darken.+(#[\w\d]+),\s+(\d+%)\s+\)");
  55.             var matches = darkenRegex.Matches(scss);
  56.  
  57.             foreach (Match match in matches)
  58.             {
  59.                 var wholeMatch = match.Groups[0].Value;
  60.                 var color = match.Groups[1].Value;
  61.                 var percentage = match.Groups[2].Value;
  62.  
  63.                 var rgbColor = this.HexToRgb(color);
  64.                 var hslColor = this.RgbToHsl(rgbColor.R, rgbColor.G, rgbColor.B);
  65.  
  66.                 var luminance = hslColor.L;
  67.  
  68.                 scss = scss.Replace(wholeMatch, color);
  69.             }
  70.  
  71.             return scss;
  72.         }
  73.  
  74.         private RgbColor HexToRgb(string hex)
  75.         {
  76.             hex = hex.Trim('#');
  77.  
  78.             return new RgbColor
  79.             {
  80.                 R = Convert.ToInt32(hex.Substring(0, 2), 16),
  81.                 G = Convert.ToInt32(hex.Substring(2, 2), 16),
  82.                 B = Convert.ToInt32(hex.Substring(4, 2), 16)
  83.             };
  84.         }
  85.  
  86.         private string RgbToHex(RgbColor rgb)
  87.         {
  88.             return '#' + rgb.R.ToString("X") + rgb.G.ToString("X") + rgb.B.ToString("X");
  89.         }
  90.  
  91.         private HslColor RgbToHsl(double r, double g, double b)
  92.         {
  93.             r /= 255;
  94.             g /= 255;
  95.             b /= 255;
  96.             double max = Math.Max(r, Math.Max(g, b));
  97.             double min = Math.Min(r, Math.Min(g, b));
  98.             double h = 0.0;
  99.             double s = 0.0;
  100.             double l = (max + min) / 2;
  101.  
  102.             if (max == min)
  103.             {
  104.                 h = s = 0.0; // achromatic
  105.             }
  106.             else
  107.             {
  108.                 double d = max - min;
  109.                 s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  110.  
  111.                 if (max == r)
  112.                     h = (g - b) / d + (g < b ? 6 : 0);
  113.                 if (max == g)
  114.                     h = (b - r) / d + 2;
  115.                 if (max == b)
  116.                     h = (r - g) / d + 4;
  117.  
  118.                 h /= 6;
  119.             }
  120.  
  121.             return new HslColor
  122.             {
  123.                 H = h,
  124.                 S = s,
  125.                 L = l
  126.             };
  127.         }
  128.  
  129.         private RgbColor HslToRgb(double h, double s, double l)
  130.         {
  131.             int r, g, b;
  132.  
  133.             if (s == 0)
  134.             {
  135.                 r = g = b = (int)l;
  136.             }
  137.             else
  138.             {
  139.                 var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  140.                 var p = 2 * l - q;
  141.                 r = Hue2rgb(p, q, h + 1 / 3);
  142.                 g = Hue2rgb(p, q, h);
  143.                 b = Hue2rgb(p, q, h - 1 / 3);
  144.             }
  145.  
  146.             return new RgbColor
  147.             {
  148.                 R = (int)Math.Round((double)r * 255),
  149.                 G = (int)Math.Round((double)g * 255),
  150.                 B = (int)Math.Round((double)b * 255)
  151.             };
  152.         }
  153.  
  154.         private int Hue2rgb(double p, double q, double t)
  155.         {
  156.             if (t < 0) t += 1;
  157.             if (t > 1) t -= 1;
  158.             if (t < 1 / 6) return (int)(p + (q - p) * 6 * t);
  159.             if (t < 1 / 2) return (int)q;
  160.             if (t < 2 / 3) return (int)(p + (q - p) * (2 / 3 - t) * 6);
  161.  
  162.             return (int)p;
  163.         }
  164.  
  165.         private class HslColor
  166.         {
  167.             public double H { get; set; }
  168.             public double S { get; set; }
  169.             public double L { get; set; }
  170.         }
  171.  
  172.         private class RgbColor
  173.         {
  174.             public int R { get; set; }
  175.             public int G { get; set; }
  176.             public int B { get; set; }
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement