David PHAM-VAN

Add some color functions

  1 +# 1.3.5
  2 +* Add some color functions
  3 +* Remove color constants from PdfColor, use PdfColors
  4 +
1 # 1.3.4 5 # 1.3.4
2 * Add available dimensions for PdfPageFormat 6 * Add available dimensions for PdfPageFormat
3 * Add Document properties 7 * Add Document properties
@@ -17,13 +17,17 @@ @@ -17,13 +17,17 @@
17 part of pdf; 17 part of pdf;
18 18
19 class PdfColor { 19 class PdfColor {
20 - const PdfColor(this.r, this.g, this.b, [this.a = 1.0]); 20 + const PdfColor(this.red, this.green, this.blue, [this.alpha = 1.0])
  21 + : assert(red >= 0 && red <= 1),
  22 + assert(green >= 0 && green <= 1),
  23 + assert(blue >= 0 && blue <= 1),
  24 + assert(alpha >= 0 && alpha <= 1);
21 25
22 const PdfColor.fromInt(int color) 26 const PdfColor.fromInt(int color)
23 - : r = (color >> 16 & 0xff) / 255.0,  
24 - g = (color >> 8 & 0xff) / 255.0,  
25 - b = (color & 0xff) / 255.0,  
26 - a = (color >> 24 & 0xff) / 255.0; 27 + : red = (color >> 16 & 0xff) / 255.0,
  28 + green = (color >> 8 & 0xff) / 255.0,
  29 + blue = (color & 0xff) / 255.0,
  30 + alpha = (color >> 24 & 0xff) / 255.0;
27 31
28 factory PdfColor.fromHex(String color) { 32 factory PdfColor.fromHex(String color) {
29 if (color.startsWith('#')) { 33 if (color.startsWith('#')) {
@@ -36,73 +40,30 @@ class PdfColor { @@ -36,73 +40,30 @@ class PdfColor {
36 (int.parse(color.substring(6, 7), radix: 16) >> 24 & 0xff) / 255.0); 40 (int.parse(color.substring(6, 7), radix: 16) >> 24 & 0xff) / 255.0);
37 } 41 }
38 42
39 - final double a;  
40 - final double r;  
41 - final double g;  
42 - final double b;  
43 -  
44 - @deprecated  
45 - static const PdfColor black = PdfColors.black;  
46 - @deprecated  
47 - static const PdfColor white = PdfColors.white;  
48 - @deprecated  
49 - static const PdfColor red = PdfColors.red;  
50 - @deprecated  
51 - static const PdfColor pink = PdfColors.pink;  
52 - @deprecated  
53 - static const PdfColor purple = PdfColors.purple;  
54 - @deprecated  
55 - static const PdfColor deepPurple = PdfColors.deepPurple;  
56 - @deprecated  
57 - static const PdfColor indigo = PdfColors.indigo;  
58 - @deprecated  
59 - static const PdfColor blue = PdfColors.blue;  
60 - @deprecated  
61 - static const PdfColor lightBlue = PdfColors.lightBlue;  
62 - @deprecated  
63 - static const PdfColor cyan = PdfColors.cyan;  
64 - @deprecated  
65 - static const PdfColor teal = PdfColors.teal;  
66 - @deprecated  
67 - static const PdfColor green = PdfColors.green;  
68 - @deprecated  
69 - static const PdfColor lightGreen = PdfColors.lightGreen;  
70 - @deprecated  
71 - static const PdfColor lime = PdfColors.lime;  
72 - @deprecated  
73 - static const PdfColor yellow = PdfColors.yellow;  
74 - @deprecated  
75 - static const PdfColor amber = PdfColors.amber;  
76 - @deprecated  
77 - static const PdfColor orange = PdfColors.orange;  
78 - @deprecated  
79 - static const PdfColor deepOrange = PdfColors.deepOrange;  
80 - @deprecated  
81 - static const PdfColor brown = PdfColors.brown;  
82 - @deprecated  
83 - static const PdfColor grey = PdfColors.grey;  
84 - @deprecated  
85 - static const PdfColor blueGrey = PdfColors.blueGrey; 43 + final double alpha;
  44 + final double red;
  45 + final double green;
  46 + final double blue;
86 47
87 int toInt() => 48 int toInt() =>
88 - ((((a * 255.0).round() & 0xff) << 24) |  
89 - (((r * 255.0).round() & 0xff) << 16) |  
90 - (((g * 255.0).round() & 0xff) << 8) |  
91 - (((b * 255.0).round() & 0xff) << 0)) & 49 + ((((alpha * 255.0).round() & 0xff) << 24) |
  50 + (((red * 255.0).round() & 0xff) << 16) |
  51 + (((green * 255.0).round() & 0xff) << 8) |
  52 + (((blue * 255.0).round() & 0xff) << 0)) &
92 0xFFFFFFFF; 53 0xFFFFFFFF;
93 54
94 String toHex() => '#' + toInt().toRadixString(16); 55 String toHex() => '#' + toInt().toRadixString(16);
95 56
96 PdfColorCmyk toCmyk() { 57 PdfColorCmyk toCmyk() {
97 - return PdfColorCmyk.fromRgb(r, g, b, a); 58 + return PdfColorCmyk.fromRgb(red, green, blue, alpha);
98 } 59 }
99 60
100 PdfColorHsv toHsv() { 61 PdfColorHsv toHsv() {
101 - return PdfColorHsv.fromRgb(r, g, b, a); 62 + return PdfColorHsv.fromRgb(red, green, blue, alpha);
102 } 63 }
103 64
104 PdfColorHsl toHsl() { 65 PdfColorHsl toHsl() {
105 - return PdfColorHsl.fromRgb(r, g, b, a); 66 + return PdfColorHsl.fromRgb(red, green, blue, alpha);
106 } 67 }
107 68
108 static double _linearizeColorComponent(double component) { 69 static double _linearizeColorComponent(double component) {
@@ -113,35 +74,50 @@ class PdfColor { @@ -113,35 +74,50 @@ class PdfColor {
113 } 74 }
114 75
115 double get luminance { 76 double get luminance {
116 - final double R = _linearizeColorComponent(r);  
117 - final double G = _linearizeColorComponent(g);  
118 - final double B = _linearizeColorComponent(b); 77 + final double R = _linearizeColorComponent(red);
  78 + final double G = _linearizeColorComponent(green);
  79 + final double B = _linearizeColorComponent(blue);
119 return 0.2126 * R + 0.7152 * G + 0.0722 * B; 80 return 0.2126 * R + 0.7152 * G + 0.0722 * B;
120 } 81 }
121 82
  83 + /// Get a complementary color with hue shifted by -120°
  84 + PdfColor get complementary => toHsv().complementary;
  85 +
  86 + /// Get some similar colors
  87 + List<PdfColor> get monochromatic => toHsv().monochromatic;
  88 +
  89 + List<PdfColor> get splitcomplementary => toHsv().splitcomplementary;
  90 +
  91 + List<PdfColor> get tetradic => toHsv().tetradic;
  92 +
  93 + List<PdfColor> get triadic => toHsv().triadic;
  94 +
  95 + List<PdfColor> get analagous => toHsv().analagous;
  96 +
122 @override 97 @override
123 - String toString() => '$runtimeType($r, $g, $b, $a)'; 98 + String toString() => '$runtimeType($red, $green, $blue, $alpha)';
124 } 99 }
125 100
126 class PdfColorCmyk extends PdfColor { 101 class PdfColorCmyk extends PdfColor {
127 - const PdfColorCmyk(this.c, this.m, this.y, this.k, [double a = 1.0])  
128 - : super((1.0 - c) * (1.0 - k), (1.0 - m) * (1.0 - k),  
129 - (1.0 - y) * (1.0 - k), a); 102 + const PdfColorCmyk(this.cyan, this.magenta, this.yellow, this.black,
  103 + [double a = 1.0])
  104 + : super((1.0 - cyan) * (1.0 - black), (1.0 - magenta) * (1.0 - black),
  105 + (1.0 - yellow) * (1.0 - black), a);
130 106
131 const PdfColorCmyk.fromRgb(double r, double g, double b, [double a = 1.0]) 107 const PdfColorCmyk.fromRgb(double r, double g, double b, [double a = 1.0])
132 - : k = 1.0 - r > g ? r : g > b ? r > g ? r : g : b,  
133 - c = (1.0 - r - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) / 108 + : black = 1.0 - r > g ? r : g > b ? r > g ? r : g : b,
  109 + cyan = (1.0 - r - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
134 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)), 110 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
135 - m = (1.0 - g - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) / 111 + magenta = (1.0 - g - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
136 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)), 112 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
137 - y = (1.0 - b - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) / 113 + yellow = (1.0 - b - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
138 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)), 114 (1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
139 super(r, g, b, a); 115 super(r, g, b, a);
140 116
141 - final double c;  
142 - final double m;  
143 - final double y;  
144 - final double k; 117 + final double cyan;
  118 + final double magenta;
  119 + final double yellow;
  120 + final double black;
145 121
146 @override 122 @override
147 PdfColorCmyk toCmyk() { 123 PdfColorCmyk toCmyk() {
@@ -149,7 +125,7 @@ class PdfColorCmyk extends PdfColor { @@ -149,7 +125,7 @@ class PdfColorCmyk extends PdfColor {
149 } 125 }
150 126
151 @override 127 @override
152 - String toString() => '$runtimeType($c, $m, $y, $k, $a)'; 128 + String toString() => '$runtimeType($cyan, $magenta, $yellow, $black, $alpha)';
153 } 129 }
154 130
155 double _getHue( 131 double _getHue(
@@ -170,6 +146,9 @@ double _getHue( @@ -170,6 +146,9 @@ double _getHue(
170 return hue; 146 return hue;
171 } 147 }
172 148
  149 +/// Same as HSB, Cylindrical geometries with hue, their angular dimension,
  150 +/// starting at the red primary at 0°, passing through the green primary
  151 +/// at 120° and the blue primary at 240°, and then wrapping back to red at 360°
173 class PdfColorHsv extends PdfColor { 152 class PdfColorHsv extends PdfColor {
174 factory PdfColorHsv(double hue, double saturation, double value, 153 factory PdfColorHsv(double hue, double saturation, double value,
175 [double alpha = 1.0]) { 154 [double alpha = 1.0]) {
@@ -213,7 +192,10 @@ class PdfColorHsv extends PdfColor { @@ -213,7 +192,10 @@ class PdfColorHsv extends PdfColor {
213 192
214 const PdfColorHsv._(this.hue, this.saturation, this.value, double red, 193 const PdfColorHsv._(this.hue, this.saturation, this.value, double red,
215 double green, double blue, double alpha) 194 double green, double blue, double alpha)
216 - : super(red, green, blue, alpha); 195 + : assert(hue >= 0 && hue < 360),
  196 + assert(saturation >= 0 && saturation <= 1),
  197 + assert(value >= 0 && value <= 1),
  198 + super(red, green, blue, alpha);
217 199
218 factory PdfColorHsv.fromRgb(double red, double green, double blue, 200 factory PdfColorHsv.fromRgb(double red, double green, double blue,
219 [double alpha]) { 201 [double alpha]) {
@@ -227,8 +209,13 @@ class PdfColorHsv extends PdfColor { @@ -227,8 +209,13 @@ class PdfColorHsv extends PdfColor {
227 return PdfColorHsv._(hue, saturation, max, red, green, blue, alpha); 209 return PdfColorHsv._(hue, saturation, max, red, green, blue, alpha);
228 } 210 }
229 211
  212 + /// Angular position the colorspace coordinate diagram in degrees from 0° to 360°
230 final double hue; 213 final double hue;
  214 +
  215 + /// Saturation of the color
231 final double saturation; 216 final double saturation;
  217 +
  218 + /// Brightness
232 final double value; 219 final double value;
233 220
234 @override 221 @override
@@ -236,8 +223,59 @@ class PdfColorHsv extends PdfColor { @@ -236,8 +223,59 @@ class PdfColorHsv extends PdfColor {
236 return this; 223 return this;
237 } 224 }
238 225
  226 + /// Get a complementary color with hue shifted by -120°
  227 + @override
  228 + PdfColorHsv get complementary =>
  229 + PdfColorHsv((hue - 120) % 360, saturation, value, alpha);
  230 +
  231 + /// Get a similar color
  232 + @override
  233 + List<PdfColorHsv> get monochromatic => <PdfColorHsv>[
  234 + PdfColorHsv(
  235 + hue,
  236 + (saturation > 0.5 ? saturation - 0.2 : saturation + 0.2)
  237 + .clamp(0, 1),
  238 + (value > 0.5 ? value - 0.1 : value + 0.1).clamp(0, 1)),
  239 + PdfColorHsv(
  240 + hue,
  241 + (saturation > 0.5 ? saturation - 0.4 : saturation + 0.4)
  242 + .clamp(0, 1),
  243 + (value > 0.5 ? value - 0.2 : value + 0.2).clamp(0, 1)),
  244 + PdfColorHsv(
  245 + hue,
  246 + (saturation > 0.5 ? saturation - 0.15 : saturation + 0.15)
  247 + .clamp(0, 1),
  248 + (value > 0.5 ? value - 0.05 : value + 0.05).clamp(0, 1))
  249 + ];
  250 +
  251 + /// Get two complementary colors with hue shifted by -120°
  252 + @override
  253 + List<PdfColorHsv> get splitcomplementary => <PdfColorHsv>[
  254 + PdfColorHsv((hue - 150) % 360, saturation, value, alpha),
  255 + PdfColorHsv((hue - 180) % 360, saturation, value, alpha),
  256 + ];
  257 +
  258 + @override
  259 + List<PdfColorHsv> get triadic => <PdfColorHsv>[
  260 + PdfColorHsv((hue + 80) % 360, saturation, value, alpha),
  261 + PdfColorHsv((hue - 120) % 360, saturation, value, alpha),
  262 + ];
  263 +
  264 + @override
  265 + List<PdfColorHsv> get tetradic => <PdfColorHsv>[
  266 + PdfColorHsv((hue + 120) % 360, saturation, value, alpha),
  267 + PdfColorHsv((hue - 150) % 360, saturation, value, alpha),
  268 + PdfColorHsv((hue + 60) % 360, saturation, value, alpha),
  269 + ];
  270 +
  271 + @override
  272 + List<PdfColorHsv> get analagous => <PdfColorHsv>[
  273 + PdfColorHsv((hue + 30) % 360, saturation, value, alpha),
  274 + PdfColorHsv((hue - 20) % 360, saturation, value, alpha),
  275 + ];
  276 +
239 @override 277 @override
240 - String toString() => '$runtimeType($hue, $saturation, $value, $a)'; 278 + String toString() => '$runtimeType($hue, $saturation, $value, $alpha)';
241 } 279 }
242 280
243 class PdfColorHsl extends PdfColor { 281 class PdfColorHsl extends PdfColor {
@@ -309,5 +347,5 @@ class PdfColorHsl extends PdfColor { @@ -309,5 +347,5 @@ class PdfColorHsl extends PdfColor {
309 } 347 }
310 348
311 @override 349 @override
312 - String toString() => '$runtimeType($hue, $saturation, $lightness, $a)'; 350 + String toString() => '$runtimeType($hue, $saturation, $lightness, $alpha)';
313 } 351 }
@@ -107,13 +107,59 @@ class PDFColor extends PdfColor { @@ -107,13 +107,59 @@ class PDFColor extends PdfColor {
107 107
108 factory PDFColor.fromInt(int color) { 108 factory PDFColor.fromInt(int color) {
109 final PdfColor c = PdfColor.fromInt(color); 109 final PdfColor c = PdfColor.fromInt(color);
110 - return PDFColor(c.r, c.g, c.b, c.a); 110 + return PDFColor(c.red, c.green, c.blue, c.alpha);
111 } 111 }
112 112
113 factory PDFColor.fromHex(String color) { 113 factory PDFColor.fromHex(String color) {
114 final PdfColor c = PdfColor.fromHex(color); 114 final PdfColor c = PdfColor.fromHex(color);
115 - return PDFColor(c.r, c.g, c.b, c.a); 115 + return PDFColor(c.red, c.green, c.blue, c.alpha);
116 } 116 }
  117 +
  118 + @deprecated
  119 + static const PdfColor black = PdfColors.black;
  120 + @deprecated
  121 + static const PdfColor white = PdfColors.white;
  122 + @deprecated
  123 + static const PdfColor pink = PdfColors.pink;
  124 + @deprecated
  125 + static const PdfColor purple = PdfColors.purple;
  126 + @deprecated
  127 + static const PdfColor deepPurple = PdfColors.deepPurple;
  128 + @deprecated
  129 + static const PdfColor indigo = PdfColors.indigo;
  130 + @deprecated
  131 + static const PdfColor lightBlue = PdfColors.lightBlue;
  132 + @deprecated
  133 + static const PdfColor cyan = PdfColors.cyan;
  134 + @deprecated
  135 + static const PdfColor teal = PdfColors.teal;
  136 + @deprecated
  137 + static const PdfColor lightGreen = PdfColors.lightGreen;
  138 + @deprecated
  139 + static const PdfColor lime = PdfColors.lime;
  140 + @deprecated
  141 + static const PdfColor yellow = PdfColors.yellow;
  142 + @deprecated
  143 + static const PdfColor amber = PdfColors.amber;
  144 + @deprecated
  145 + static const PdfColor orange = PdfColors.orange;
  146 + @deprecated
  147 + static const PdfColor deepOrange = PdfColors.deepOrange;
  148 + @deprecated
  149 + static const PdfColor brown = PdfColors.brown;
  150 + @deprecated
  151 + static const PdfColor grey = PdfColors.grey;
  152 + @deprecated
  153 + static const PdfColor blueGrey = PdfColors.blueGrey;
  154 +
  155 + @deprecated
  156 + double get r => red;
  157 +
  158 + @deprecated
  159 + double get g => green;
  160 +
  161 + @deprecated
  162 + double get b => blue;
117 } 163 }
118 164
119 @deprecated 165 @deprecated
@@ -212,10 +212,11 @@ class PdfGraphics { @@ -212,10 +212,11 @@ class PdfGraphics {
212 /// @param c Color to use 212 /// @param c Color to use
213 void setFillColor(PdfColor color) { 213 void setFillColor(PdfColor color) {
214 if (color is PdfColorCmyk) { 214 if (color is PdfColorCmyk) {
215 - buf.putNumList(<double>[color.c, color.m, color.y, color.k]); 215 + buf.putNumList(
  216 + <double>[color.cyan, color.magenta, color.yellow, color.black]);
216 buf.putString(' k\n'); 217 buf.putString(' k\n');
217 } else { 218 } else {
218 - buf.putNumList(<double>[color.r, color.g, color.b]); 219 + buf.putNumList(<double>[color.red, color.green, color.blue]);
219 buf.putString(' rg\n'); 220 buf.putString(' rg\n');
220 } 221 }
221 } 222 }
@@ -225,10 +226,11 @@ class PdfGraphics { @@ -225,10 +226,11 @@ class PdfGraphics {
225 /// @param c Color to use 226 /// @param c Color to use
226 void setStrokeColor(PdfColor color) { 227 void setStrokeColor(PdfColor color) {
227 if (color is PdfColorCmyk) { 228 if (color is PdfColorCmyk) {
228 - buf.putNumList(<double>[color.c, color.m, color.y, color.k]); 229 + buf.putNumList(
  230 + <double>[color.cyan, color.magenta, color.yellow, color.black]);
229 buf.putString(' K\n'); 231 buf.putString(' K\n');
230 } else { 232 } else {
231 - buf.putNumList(<double>[color.r, color.g, color.b]); 233 + buf.putNumList(<double>[color.red, color.green, color.blue]);
232 buf.putString(' RG\n'); 234 buf.putString(' RG\n');
233 } 235 }
234 } 236 }
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 1.3.4 7 +version: 1.3.5
8 8
9 environment: 9 environment:
10 sdk: ">=2.1.0 <3.0.0" 10 sdk: ">=2.1.0 <3.0.0"
@@ -44,6 +44,13 @@ class Color extends StatelessWidget { @@ -44,6 +44,13 @@ class Color extends StatelessWidget {
44 Text(varient ?? '', style: style), 44 Text(varient ?? '', style: style),
45 Padding(padding: const EdgeInsets.all(2 * PdfPageFormat.mm)), 45 Padding(padding: const EdgeInsets.all(2 * PdfPageFormat.mm)),
46 Text(color.toHex(), style: hexStyle), 46 Text(color.toHex(), style: hexStyle),
  47 + Row(
  48 + mainAxisSize: MainAxisSize.max,
  49 + mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  50 + children: color.monochromatic
  51 + .map<Widget>((PdfColor color) =>
  52 + Container(width: 30, height: 30, color: color))
  53 + .toList())
47 ], 54 ],
48 // ) 55 // )
49 )); 56 ));