diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2016-07-15 14:09:07 +0300 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2016-07-15 14:09:17 +0300 |
commit | ccb3015c030164fd5b9ea72d081f5eb6ccb8207e (patch) | |
tree | b22f75483cfdbce9674870da218c37e0add2733d /main/src/core/Mono.Texteditor | |
parent | 4bc207f8d12aac43a487115926cf770d2a7d5503 (diff) |
[Ide] Optimized hsl color conversion.
Diffstat (limited to 'main/src/core/Mono.Texteditor')
-rw-r--r-- | main/src/core/Mono.Texteditor/Mono.TextEditor/Gui/HslColor.cs | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/main/src/core/Mono.Texteditor/Mono.TextEditor/Gui/HslColor.cs b/main/src/core/Mono.Texteditor/Mono.TextEditor/Gui/HslColor.cs index f8e525f939..8960a886e5 100644 --- a/main/src/core/Mono.Texteditor/Mono.TextEditor/Gui/HslColor.cs +++ b/main/src/core/Mono.Texteditor/Mono.TextEditor/Gui/HslColor.cs @@ -36,79 +36,77 @@ namespace Mono.TextEditor get; set; } - + public double S { get; set; } - + public double L { get; set; } - - void ToRgb(out double r, out double g, out double b) + + void ToRgb (out double r, out double g, out double b) { if (L == 0) { r = g = b = 0; return; } - + if (S == 0) { r = g = b = L; } else { - double temp2 = L <= 0.5 ? L * (1.0 + S) : L + S -(L * S); + double temp2 = L <= 0.5 ? L * (1.0 + S) : L + S - (L * S); double temp1 = 2.0 * L - temp2; - - double[] t3 = new double[] { H + 1.0 / 3.0, H, H - 1.0 / 3.0}; - double[] clr= new double[] { 0, 0, 0}; - for (int i = 0; i < 3; i++) { - if (t3[i] < 0) - t3[i] += 1.0; - if (t3[i] > 1) - t3[i]-=1.0; - if (6.0 * t3[i] < 1.0) - clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0; - else if (2.0 * t3[i] < 1.0) - clr[i] = temp2; - else if (3.0 * t3[i] < 2.0) - clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0); - else - clr[i] = temp1; - } - - r = clr[0]; - g = clr[1]; - b = clr[2]; + + r = ConvertVector (temp2, temp1, H + 1.0 / 3.0); + g = ConvertVector (temp2, temp1, H); + b = ConvertVector (temp2, temp1, H - 1.0 / 3.0); } } - + + static double ConvertVector (double temp2, double temp1, double x) + { + if (x < 0) + x += 1.0; + if (x > 1) + x -= 1.0; + if (6.0 * x < 1.0) + return temp1 + (temp2 - temp1) * x * 6.0; + if (2.0 * x < 1.0) + return temp2; + if (3.0 * x < 2.0) + return (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - x) * 6.0); + return temp1; + } + public static implicit operator Color (HslColor hsl) { double r = 0, g = 0, b = 0; hsl.ToRgb (out r, out g, out b); - return new Color ((byte)(255 * r), - (byte)(255 * g), - (byte)(255 * b)); + return new Color ((byte)(255 * r), + (byte)(255 * g), + (byte)(255 * b)); } - + public static implicit operator Cairo.Color (HslColor hsl) { double r = 0, g = 0, b = 0; hsl.ToRgb (out r, out g, out b); return new Cairo.Color (r, g, b); } - + public static implicit operator HslColor (Color color) { return new HslColor (color); } - + public static implicit operator HslColor (Cairo.Color color) { return new HslColor (color); } - + public static HslColor FromHsl (double h, double s, double l) { return new HslColor () { @@ -121,7 +119,7 @@ namespace Mono.TextEditor public uint ToPixel () { double r, g, b; - ToRgb(out r, out g, out b); + ToRgb (out r, out g, out b); uint rv = (uint)(r * 255); uint gv = (uint)(g * 255); uint bv = (uint)(b * 255); @@ -135,7 +133,7 @@ namespace Mono.TextEditor var b = (pixel & 0xFF) / 255.0; return new HslColor (r, g, b); } - + public HslColor (double r, double g, double b) : this () { double v = System.Math.Max (r, g); @@ -143,23 +141,23 @@ namespace Mono.TextEditor double m = System.Math.Min (r, g); m = System.Math.Min (m, b); - + this.L = (m + v) / 2.0; if (this.L <= 0.0) return; double vm = v - m; this.S = vm; - + if (this.S > 0.0) { this.S /= (this.L <= 0.5) ? (v + m) : (2.0 - v - m); } else { return; } - + double r2 = (v - r) / vm; double g2 = (v - g) / vm; double b2 = (v - b) / vm; - + if (r == v) { this.H = (g == m ? 5.0 + b2 : 1.0 - g2); } else if (g == v) { @@ -169,15 +167,15 @@ namespace Mono.TextEditor } this.H /= 6.0; } - + public HslColor (Color color) : this (color.Red / (double)ushort.MaxValue, color.Green / (double)ushort.MaxValue, color.Blue / (double)ushort.MaxValue) { } - + public HslColor (Cairo.Color color) : this (color.R, color.G, color.B) { } - + public static HslColor Parse (string color) { Gdk.Color col = new Gdk.Color (0, 0, 0); @@ -205,21 +203,21 @@ namespace Mono.TextEditor double b = c.Blue / (double)ushort.MaxValue; return System.Math.Sqrt (r * .241 + g * .691 + b * .068); } - + public static List<HslColor> GenerateHighlightColors (HslColor backGround, HslColor foreGround, int n) { double bgH = (backGround.H == 0 && backGround.S == 0) ? 2 / 3.0 : backGround.H; var result = new List<HslColor> (); for (int i = 0; i < n; i++) { double h = bgH + (i + 1.0) / (double)n; - + // for monochromatic backround the h value doesn't matter if (i + 1 == n && !(backGround.H == 0 && backGround.S == 0)) h = bgH + 0.5; - + if (h > 1.0) h -= 1.0; - + double s = 0.85; double l = 0.5; if (backGround.H == 0 && backGround.S == 0 && backGround.L < 0.5) @@ -228,18 +226,18 @@ namespace Mono.TextEditor } return result; } - + public override string ToString () { return string.Format ("[HslColor: H={0}, S={1}, L={2}]", H, S, L); } - + public string ToPangoString () { var resultColor = (Cairo.Color)this; return string.Format ("#{0:x2}{1:x2}{2:x2}", (int)(resultColor.R * 255), - (int)(resultColor.G * 255), + (int)(resultColor.G * 255), (int)(resultColor.B * 255)); } } |