David PHAM-VAN

Fix PdfColors.fromHex()

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 4
5 - Improve Table.fromTextArray() 5 - Improve Table.fromTextArray()
6 - Add curved LineDataSet Chart 6 - Add curved LineDataSet Chart
  7 +- Fix PdfColors.fromHex()
7 8
8 ## 1.7.1 9 ## 1.7.1
9 10
@@ -19,27 +19,56 @@ @@ -19,27 +19,56 @@
19 part of pdf; 19 part of pdf;
20 20
21 class PdfColor { 21 class PdfColor {
  22 + /// Create a color with red, green, blue and alpha components
  23 + /// values between 0 and 1
22 const PdfColor(this.red, this.green, this.blue, [this.alpha = 1.0]) 24 const PdfColor(this.red, this.green, this.blue, [this.alpha = 1.0])
23 : assert(red >= 0 && red <= 1), 25 : assert(red >= 0 && red <= 1),
24 assert(green >= 0 && green <= 1), 26 assert(green >= 0 && green <= 1),
25 assert(blue >= 0 && blue <= 1), 27 assert(blue >= 0 && blue <= 1),
26 assert(alpha >= 0 && alpha <= 1); 28 assert(alpha >= 0 && alpha <= 1);
27 29
  30 + /// Return a color with: 0xAARRGGBB
28 const PdfColor.fromInt(int color) 31 const PdfColor.fromInt(int color)
29 : red = (color >> 16 & 0xff) / 255.0, 32 : red = (color >> 16 & 0xff) / 255.0,
30 green = (color >> 8 & 0xff) / 255.0, 33 green = (color >> 8 & 0xff) / 255.0,
31 blue = (color & 0xff) / 255.0, 34 blue = (color & 0xff) / 255.0,
32 alpha = (color >> 24 & 0xff) / 255.0; 35 alpha = (color >> 24 & 0xff) / 255.0;
33 36
  37 + /// Can parse colors in the form:
  38 + /// * #RRGGBBAA
  39 + /// * #RRGGBB
  40 + /// * #RGB
  41 + /// * RRGGBBAA
  42 + /// * RRGGBB
  43 + /// * RGB
34 factory PdfColor.fromHex(String color) { 44 factory PdfColor.fromHex(String color) {
35 if (color.startsWith('#')) { 45 if (color.startsWith('#')) {
36 color = color.substring(1); 46 color = color.substring(1);
37 } 47 }
38 - return PdfColor(  
39 - (int.parse(color.substring(0, 1), radix: 16) >> 16 & 0xff) / 255.0,  
40 - (int.parse(color.substring(2, 3), radix: 16) >> 8 & 0xff) / 255.0,  
41 - (int.parse(color.substring(4, 5), radix: 16) & 0xff) / 255.0,  
42 - (int.parse(color.substring(6, 7), radix: 16) >> 24 & 0xff) / 255.0); 48 +
  49 + double red;
  50 + double green;
  51 + double blue;
  52 + double alpha = 1;
  53 +
  54 + if (color.length == 3) {
  55 + red = int.parse(color.substring(0, 1) * 2, radix: 16) / 255;
  56 + green = int.parse(color.substring(1, 2) * 2, radix: 16) / 255;
  57 + blue = int.parse(color.substring(2, 3) * 2, radix: 16) / 255;
  58 + return PdfColor(red, green, blue, alpha);
  59 + }
  60 +
  61 + assert(color.length == 3 || color.length == 6 || color.length == 8);
  62 +
  63 + red = int.parse(color.substring(0, 2), radix: 16) / 255;
  64 + green = int.parse(color.substring(2, 4), radix: 16) / 255;
  65 + blue = int.parse(color.substring(4, 6), radix: 16) / 255;
  66 +
  67 + if (color.length == 8) {
  68 + alpha = int.parse(color.substring(6, 8), radix: 16) / 255;
  69 + }
  70 +
  71 + return PdfColor(red, green, blue, alpha);
43 } 72 }
44 73
45 factory PdfColor.fromRYB(double red, double yellow, double blue, 74 factory PdfColor.fromRYB(double red, double yellow, double blue,
@@ -113,7 +142,12 @@ class PdfColor { @@ -113,7 +142,12 @@ class PdfColor {
113 (((blue * 255.0).round() & 0xff) << 0)) & 142 (((blue * 255.0).round() & 0xff) << 0)) &
114 0xFFFFFFFF; 143 0xFFFFFFFF;
115 144
116 - String toHex() => '#' + toInt().toRadixString(16); 145 + String toHex() {
  146 + final int i = toInt();
  147 + final String rgb = (i & 0xffffff).toRadixString(16);
  148 + final String a = ((i & 0xff000000) >> 24).toRadixString(16);
  149 + return '#$rgb$a';
  150 + }
117 151
118 PdfColorCmyk toCmyk() { 152 PdfColorCmyk toCmyk() {
119 return PdfColorCmyk.fromRgb(red, green, blue, alpha); 153 return PdfColorCmyk.fromRgb(red, green, blue, alpha);
@@ -45,7 +45,7 @@ class Color extends StatelessWidget { @@ -45,7 +45,7 @@ class Color extends StatelessWidget {
45 children: <Widget>[ 45 children: <Widget>[
46 Text(name, style: style), 46 Text(name, style: style),
47 Text(varient ?? '', style: style), 47 Text(varient ?? '', style: style),
48 - Padding(padding: const EdgeInsets.all(2 * PdfPageFormat.mm)), 48 + SizedBox(height: 4 * PdfPageFormat.mm),
49 Text(color.toHex(), style: hexStyle), 49 Text(color.toHex(), style: hexStyle),
50 Row( 50 Row(
51 mainAxisSize: MainAxisSize.max, 51 mainAxisSize: MainAxisSize.max,
@@ -709,6 +709,44 @@ void main() { @@ -709,6 +709,44 @@ void main() {
709 ])); 709 ]));
710 }); 710 });
711 711
  712 + group('Pdf Colors Conversions', () {
  713 + test('fromHex #RRGGBBAA', () {
  714 + final PdfColor c = PdfColor.fromHex('#12345678');
  715 + expect(c.red, 0x12 / 255);
  716 + expect(c.green, 0x34 / 255);
  717 + expect(c.blue, 0x56 / 255);
  718 + expect(c.alpha, 0x78 / 255);
  719 + });
  720 +
  721 + test('fromHex RRGGBBAA', () {
  722 + final PdfColor c = PdfColor.fromHex('12345678');
  723 + expect(c.red, 0x12 / 255);
  724 + expect(c.green, 0x34 / 255);
  725 + expect(c.blue, 0x56 / 255);
  726 + expect(c.alpha, 0x78 / 255);
  727 + });
  728 +
  729 + test('fromHex RRGGBB', () {
  730 + final PdfColor c = PdfColor.fromHex('123456');
  731 + expect(c.red, 0x12 / 255);
  732 + expect(c.green, 0x34 / 255);
  733 + expect(c.blue, 0x56 / 255);
  734 + expect(c.alpha, 1);
  735 + });
  736 +
  737 + test('fromHex RGB', () {
  738 + final PdfColor c = PdfColor.fromHex('18f');
  739 + expect(c.red, 0x11 / 255);
  740 + expect(c.green, 0x88 / 255);
  741 + expect(c.blue, 0xff / 255);
  742 + expect(c.alpha, 1);
  743 + });
  744 +
  745 + test('toHex RGB', () {
  746 + expect(PdfColor.fromHex('#12345678').toHex(), '#12345678');
  747 + });
  748 + });
  749 +
712 tearDownAll(() { 750 tearDownAll(() {
713 final File file = File('colors.pdf'); 751 final File file = File('colors.pdf');
714 file.writeAsBytesSync(pdf.save()); 752 file.writeAsBytesSync(pdf.save());
@@ -31,7 +31,7 @@ List<TableRow> buildTable( @@ -31,7 +31,7 @@ List<TableRow> buildTable(
31 final List<TableRow> rows = <TableRow>[]; 31 final List<TableRow> rows = <TableRow>[];
32 { 32 {
33 final List<Widget> tableRow = <Widget>[]; 33 final List<Widget> tableRow = <Widget>[];
34 - for (String cell in <String>['Hue', 'Color', 'ARGB']) { 34 + for (String cell in <String>['Hue', 'Color', 'RGBA']) {
35 tableRow.add(Container( 35 tableRow.add(Container(
36 alignment: Alignment.center, 36 alignment: Alignment.center,
37 margin: const EdgeInsets.all(5), 37 margin: const EdgeInsets.all(5),
No preview for this file type