Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace WorkWave.CustomerPortal.Services.Utils
- {
- public class ScssCompiler
- {
- private Dictionary<string, string> scssVariables = new Dictionary<string, string>();
- public string Compile(string scss)
- {
- scss = this.GetSassVariables(scss);
- scss = this.ReplaceSassVariables(scss);
- scss = this.ApplyDarkenFunction(scss);
- var a = 5;
- return "asd";
- }
- private string ReplaceSassVariables(string scss)
- {
- foreach (var pair in scssVariables)
- {
- scss = scss.Replace(pair.Key, pair.Value);
- }
- return scss;
- }
- private string GetSassVariables(string scss)
- {
- var variableRegex = new Regex(@"\$[a-z\-]+:#[\w\d]+;");
- var matches = variableRegex.Matches(scss);
- foreach (Match match in matches)
- {
- var args = match.Value.Split(':');
- var variableName = args[0];
- var variableValue = args[1].Trim(';');
- scssVariables[variableName] = variableValue;
- scss = scss.Replace(match.Value, string.Empty);
- }
- return scss;
- }
- private string ApplyDarkenFunction(string scss)
- {
- var darkenRegex = new Regex(@"darken.+(#[\w\d]+),\s+(\d+%)\s+\)");
- var matches = darkenRegex.Matches(scss);
- foreach (Match match in matches)
- {
- var wholeMatch = match.Groups[0].Value;
- var color = match.Groups[1].Value;
- var percentage = match.Groups[2].Value;
- var rgbColor = this.HexToRgb(color);
- var hslColor = this.RgbToHsl(rgbColor.R, rgbColor.G, rgbColor.B);
- var luminance = hslColor.L;
- scss = scss.Replace(wholeMatch, color);
- }
- return scss;
- }
- private RgbColor HexToRgb(string hex)
- {
- hex = hex.Trim('#');
- return new RgbColor
- {
- R = Convert.ToInt32(hex.Substring(0, 2), 16),
- G = Convert.ToInt32(hex.Substring(2, 2), 16),
- B = Convert.ToInt32(hex.Substring(4, 2), 16)
- };
- }
- private string RgbToHex(RgbColor rgb)
- {
- return '#' + rgb.R.ToString("X") + rgb.G.ToString("X") + rgb.B.ToString("X");
- }
- private HslColor RgbToHsl(double r, double g, double b)
- {
- r /= 255;
- g /= 255;
- b /= 255;
- double max = Math.Max(r, Math.Max(g, b));
- double min = Math.Min(r, Math.Min(g, b));
- double h = 0.0;
- double s = 0.0;
- double l = (max + min) / 2;
- if (max == min)
- {
- h = s = 0.0; // achromatic
- }
- else
- {
- double d = max - min;
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
- if (max == r)
- h = (g - b) / d + (g < b ? 6 : 0);
- if (max == g)
- h = (b - r) / d + 2;
- if (max == b)
- h = (r - g) / d + 4;
- h /= 6;
- }
- return new HslColor
- {
- H = h,
- S = s,
- L = l
- };
- }
- private RgbColor HslToRgb(double h, double s, double l)
- {
- int r, g, b;
- if (s == 0)
- {
- r = g = b = (int)l;
- }
- else
- {
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
- var p = 2 * l - q;
- r = Hue2rgb(p, q, h + 1 / 3);
- g = Hue2rgb(p, q, h);
- b = Hue2rgb(p, q, h - 1 / 3);
- }
- return new RgbColor
- {
- R = (int)Math.Round((double)r * 255),
- G = (int)Math.Round((double)g * 255),
- B = (int)Math.Round((double)b * 255)
- };
- }
- private int Hue2rgb(double p, double q, double t)
- {
- if (t < 0) t += 1;
- if (t > 1) t -= 1;
- if (t < 1 / 6) return (int)(p + (q - p) * 6 * t);
- if (t < 1 / 2) return (int)q;
- if (t < 2 / 3) return (int)(p + (q - p) * (2 / 3 - t) * 6);
- return (int)p;
- }
- private class HslColor
- {
- public double H { get; set; }
- public double S { get; set; }
- public double L { get; set; }
- }
- private class RgbColor
- {
- public int R { get; set; }
- public int G { get; set; }
- public int B { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement