OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Utility library for creating web colors. | 5 /// Utility library for creating web colors. |
6 | 6 |
7 library sourcemaps.colors; | 7 library sourcemaps.colors; |
8 | 8 |
9 /// A web color. | 9 /// A web color. |
10 abstract class Color { | 10 abstract class Color { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 sb.write(', '); | 65 sb.write(', '); |
66 writeInt(g); | 66 writeInt(g); |
67 sb.write(', '); | 67 sb.write(', '); |
68 writeInt(b); | 68 writeInt(b); |
69 sb.write(', '); | 69 sb.write(', '); |
70 sb.write(a); | 70 sb.write(a); |
71 sb.write(')'); | 71 sb.write(')'); |
72 | 72 |
73 return sb.toString(); | 73 return sb.toString(); |
74 } | 74 } |
75 | |
76 } | 75 } |
77 | 76 |
78 /// A web color defined as HSV. | 77 /// A web color defined as HSV. |
79 class HSV implements Color { | 78 class HSV implements Color { |
80 final double h; | 79 final double h; |
81 final double s; | 80 final double s; |
82 final double v; | 81 final double v; |
83 | 82 |
84 /// Creates a color defined by the hue [h] in range 0..360 (360 excluded), | 83 /// Creates a color defined by the hue [h] in range 0..360 (360 excluded), |
85 /// saturation [s] in range 0..1, and value [v] in range 0..1. | 84 /// saturation [s] in range 0..1, and value [v] in range 0..1. |
86 const HSV(this.h, this.s, this.v); | 85 const HSV(this.h, this.s, this.v); |
87 | 86 |
88 String get toCss => toRGB(this).toCss; | 87 String get toCss => toRGB(this).toCss; |
89 | 88 |
90 static RGB toRGB(HSV hsv) { | 89 static RGB toRGB(HSV hsv) { |
91 double h = hsv.h; | 90 double h = hsv.h; |
92 double s = hsv.s; | 91 double s = hsv.s; |
93 double v = hsv.v; | 92 double v = hsv.v; |
94 if (s == 0.0) { | 93 if (s == 0.0) { |
95 // Grey. | 94 // Grey. |
96 return new RGB(v, v, v); | 95 return new RGB(v, v, v); |
97 } | 96 } |
98 h /= 60.0; // Sector 0 to 5. | 97 h /= 60.0; // Sector 0 to 5. |
99 int i = h.floor(); | 98 int i = h.floor(); |
100 double f = h - i; // Factorial part of [h]. | 99 double f = h - i; // Factorial part of [h]. |
101 double p = v * (1.0 - s); | 100 double p = v * (1.0 - s); |
102 double q = v * (1.0 - s * f); | 101 double q = v * (1.0 - s * f); |
103 double t = v * (1.0 - s * (1.0 - f )); | 102 double t = v * (1.0 - s * (1.0 - f)); |
104 switch (i) { | 103 switch (i) { |
105 case 0: | 104 case 0: |
106 return new RGB(v, t, p); | 105 return new RGB(v, t, p); |
107 case 1: | 106 case 1: |
108 return new RGB(q, v, p); | 107 return new RGB(q, v, p); |
109 case 2: | 108 case 2: |
110 return new RGB(p, v, t); | 109 return new RGB(p, v, t); |
111 case 3: | 110 case 3: |
112 return new RGB(p, q, v); | 111 return new RGB(p, q, v); |
113 case 4: | 112 case 4: |
114 return new RGB(t, p, v); | 113 return new RGB(t, p, v); |
115 default: // case 5: | 114 default: // case 5: |
116 return new RGB(v, p, q); | 115 return new RGB(v, p, q); |
117 } | 116 } |
118 } | 117 } |
119 | 118 |
120 String toString() => 'hsv($h,$s,$v)'; | 119 String toString() => 'hsv($h,$s,$v)'; |
121 } | 120 } |
OLD | NEW |