| 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 colors; | 7 library colors; |
| 8 | 8 |
| 9 /// A web color. | 9 /// A web color. |
| 10 abstract class Color { | 10 abstract class Color { |
| 11 /// The hexadecimal code for the color, without the prefixed '#'. | 11 /// The css code for the color. |
| 12 String get toHex; | 12 String get toCss; |
| 13 } | 13 } |
| 14 | 14 |
| 15 /// A web color defined as RGB. | 15 /// A web color defined as RGB. |
| 16 class RGB implements Color { | 16 class RGB implements Color { |
| 17 final double r; | 17 final double r; |
| 18 final double g; | 18 final double g; |
| 19 final double b; | 19 final double b; |
| 20 | 20 |
| 21 /// Creates a color defined by the amount of red [r], green [g], and blue [b] | 21 /// Creates a color defined by the amount of red [r], green [g], and blue [b] |
| 22 /// all in range 0..1. | 22 /// all in range 0..1. |
| 23 const RGB(this.r, this.g, this.b); | 23 const RGB(this.r, this.g, this.b); |
| 24 | 24 |
| 25 String get toHex { | 25 String get toCss { |
| 26 StringBuffer sb = new StringBuffer(); | 26 StringBuffer sb = new StringBuffer(); |
| 27 sb.write('#'); |
| 27 | 28 |
| 28 void writeHex(double value) { | 29 void writeHex(double value) { |
| 29 int i = (value * 255.0).round(); | 30 int i = (value * 255.0).round(); |
| 30 if (i < 16) { | 31 if (i < 16) { |
| 31 sb.write('0'); | 32 sb.write('0'); |
| 32 } | 33 } |
| 33 sb.write(i.toRadixString(16)); | 34 sb.write(i.toRadixString(16)); |
| 34 } | 35 } |
| 35 | 36 |
| 36 writeHex(r); | 37 writeHex(r); |
| 37 writeHex(g); | 38 writeHex(g); |
| 38 writeHex(b); | 39 writeHex(b); |
| 39 | 40 |
| 40 return sb.toString(); | 41 return sb.toString(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 String toString() => 'rgb($r,$g,$b)'; | 44 String toString() => 'rgb($r,$g,$b)'; |
| 44 } | 45 } |
| 45 | 46 |
| 47 class RGBA extends RGB { |
| 48 final double a; |
| 49 |
| 50 const RGBA(double r, double g, double b, this.a) : super(r, g, b); |
| 51 |
| 52 String get toCss { |
| 53 StringBuffer sb = new StringBuffer(); |
| 54 |
| 55 void writeInt(double value) { |
| 56 int i = (value * 255.0).round(); |
| 57 if (i < 16) { |
| 58 sb.write('0'); |
| 59 } |
| 60 sb.write(i); |
| 61 } |
| 62 |
| 63 sb.write('rgba('); |
| 64 writeInt(r); |
| 65 sb.write(', '); |
| 66 writeInt(g); |
| 67 sb.write(', '); |
| 68 writeInt(b); |
| 69 sb.write(', '); |
| 70 sb.write(a); |
| 71 sb.write(')'); |
| 72 |
| 73 return sb.toString(); |
| 74 } |
| 75 |
| 76 } |
| 77 |
| 46 /// A web color defined as HSV. | 78 /// A web color defined as HSV. |
| 47 class HSV implements Color { | 79 class HSV implements Color { |
| 48 final double h; | 80 final double h; |
| 49 final double s; | 81 final double s; |
| 50 final double v; | 82 final double v; |
| 51 | 83 |
| 52 /// Creates a color defined by the hue [h] in range 0..360 (360 excluded), | 84 /// Creates a color defined by the hue [h] in range 0..360 (360 excluded), |
| 53 /// saturation [s] in range 0..1, and value [v] in range 0..1. | 85 /// saturation [s] in range 0..1, and value [v] in range 0..1. |
| 54 const HSV(this.h, this.s, this.v); | 86 const HSV(this.h, this.s, this.v); |
| 55 | 87 |
| 56 String get toHex => toRGB(this).toHex; | 88 String get toCss => toRGB(this).toCss; |
| 57 | 89 |
| 58 static RGB toRGB(HSV hsv) { | 90 static RGB toRGB(HSV hsv) { |
| 59 double h = hsv.h; | 91 double h = hsv.h; |
| 60 double s = hsv.s; | 92 double s = hsv.s; |
| 61 double v = hsv.v; | 93 double v = hsv.v; |
| 62 if (s == 0.0) { | 94 if (s == 0.0) { |
| 63 // Grey. | 95 // Grey. |
| 64 return new RGB(v, v, v); | 96 return new RGB(v, v, v); |
| 65 } | 97 } |
| 66 h /= 60.0; // Sector 0 to 5. | 98 h /= 60.0; // Sector 0 to 5. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 80 return new RGB(p, q, v); | 112 return new RGB(p, q, v); |
| 81 case 4: | 113 case 4: |
| 82 return new RGB(t, p, v); | 114 return new RGB(t, p, v); |
| 83 default: // case 5: | 115 default: // case 5: |
| 84 return new RGB(v, p, q); | 116 return new RGB(v, p, q); |
| 85 } | 117 } |
| 86 } | 118 } |
| 87 | 119 |
| 88 String toString() => 'hsv($h,$s,$v)'; | 120 String toString() => 'hsv($h,$s,$v)'; |
| 89 } | 121 } |
| OLD | NEW |