David PHAM-VAN

Improve PieChart default colors

@@ -83,11 +83,9 @@ class Invoice { @@ -83,11 +83,9 @@ class Invoice {
83 static const _darkColor = PdfColors.blueGrey800; 83 static const _darkColor = PdfColors.blueGrey800;
84 static const _lightColor = PdfColors.white; 84 static const _lightColor = PdfColors.white;
85 85
86 - PdfColor get _baseTextColor =>  
87 - baseColor.luminance < 0.5 ? _lightColor : _darkColor; 86 + PdfColor get _baseTextColor => baseColor.isLight ? _lightColor : _darkColor;
88 87
89 - PdfColor get _accentTextColor =>  
90 - baseColor.luminance < 0.5 ? _lightColor : _darkColor; 88 + PdfColor get _accentTextColor => baseColor.isLight ? _lightColor : _darkColor;
91 89
92 double get _total => 90 double get _total =>
93 products.map<double>((p) => p.total).reduce((a, b) => a + b); 91 products.map<double>((p) => p.total).reduce((a, b) => a + b);
@@ -282,17 +282,13 @@ Future<Uint8List> generateReport( @@ -282,17 +282,13 @@ Future<Uint8List> generateReport(
282 datasets: List<pw.Dataset>.generate(dataTable.length, (index) { 282 datasets: List<pw.Dataset>.generate(dataTable.length, (index) {
283 final data = dataTable[index]; 283 final data = dataTable[index];
284 final color = chartColors[index % chartColors.length]; 284 final color = chartColors[index % chartColors.length];
285 - final textColor =  
286 - color.luminance < 0.2 ? PdfColors.white : PdfColors.black;  
287 -  
288 final value = (data[2] as num).toDouble(); 285 final value = (data[2] as num).toDouble();
289 final pct = (value / expense * 100).round(); 286 final pct = (value / expense * 100).round();
290 -  
291 return pw.PieDataSet( 287 return pw.PieDataSet(
292 legend: '${data[0]}\n$pct%', 288 legend: '${data[0]}\n$pct%',
293 value: value, 289 value: value,
294 color: color, 290 color: color,
295 - legendStyle: pw.TextStyle(fontSize: 10, color: textColor), 291 + legendStyle: pw.TextStyle(fontSize: 10),
296 ); 292 );
297 }), 293 }),
298 ), 294 ),
@@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
13 - Passthrough SpanningWidget on SingleChildWidget and StatelessWidget 13 - Passthrough SpanningWidget on SingleChildWidget and StatelessWidget
14 - Improve TextOverflow support 14 - Improve TextOverflow support
15 - Fix Table horizontalInside borders 15 - Fix Table horizontalInside borders
  16 +- Improve PieChart default colors
16 17
17 ## 3.2.0 18 ## 3.2.0
18 19
@@ -180,6 +180,16 @@ class PdfColor { @@ -180,6 +180,16 @@ class PdfColor {
180 return math.pow((component + 0.055) / 1.055, 2.4).toDouble(); 180 return math.pow((component + 0.055) / 1.055, 2.4).toDouble();
181 } 181 }
182 182
  183 + /// Determines whether the given [PdfColor] is light.
  184 + bool get isLight => !isDark;
  185 +
  186 + /// Determines whether the given [PdfColor] is dark.
  187 + bool get isDark {
  188 + final relativeLuminance = luminance;
  189 + const kThreshold = 0.15;
  190 + return (relativeLuminance + 0.05) * (relativeLuminance + 0.05) > kThreshold;
  191 + }
  192 +
183 /// Get the luminance 193 /// Get the luminance
184 double get luminance { 194 double get luminance {
185 final R = _linearizeColorComponent(red); 195 final R = _linearizeColorComponent(red);
@@ -240,6 +250,12 @@ class PdfColor { @@ -240,6 +250,12 @@ class PdfColor {
240 int get hashCode => toInt(); 250 int get hashCode => toInt();
241 } 251 }
242 252
  253 +class PdfColorGrey extends PdfColor {
  254 + /// Create a grey color
  255 + const PdfColorGrey(double color, [double alpha = 1.0])
  256 + : super(color, color, color, alpha);
  257 +}
  258 +
243 /// Represents an CMYK color 259 /// Represents an CMYK color
244 class PdfColorCmyk extends PdfColor { 260 class PdfColorCmyk extends PdfColor {
245 /// Creates a CMYK color 261 /// Creates a CMYK color
@@ -126,13 +126,14 @@ class PieDataSet extends Dataset { @@ -126,13 +126,14 @@ class PieDataSet extends Dataset {
126 this.legendAlign, 126 this.legendAlign,
127 this.legendPosition = PieLegendPosition.auto, 127 this.legendPosition = PieLegendPosition.auto,
128 this.legendLineWidth = 1.0, 128 this.legendLineWidth = 1.0,
129 - this.legendLineColor = PdfColors.black, 129 + PdfColor? legendLineColor,
130 Widget? legendWidget, 130 Widget? legendWidget,
131 this.legendOffset = 20, 131 this.legendOffset = 20,
132 }) : drawBorder = drawBorder ?? borderColor != null && color != borderColor, 132 }) : drawBorder = drawBorder ?? borderColor != null && color != borderColor,
133 assert((drawBorder ?? borderColor != null && color != borderColor) || 133 assert((drawBorder ?? borderColor != null && color != borderColor) ||
134 drawSurface), 134 drawSurface),
135 _legendWidget = legendWidget, 135 _legendWidget = legendWidget,
  136 + legendLineColor = legendLineColor ?? color,
136 super( 137 super(
137 legend: legend, 138 legend: legend,
138 color: color, 139 color: color,
@@ -201,7 +202,18 @@ class PieDataSet extends Dataset { @@ -201,7 +202,18 @@ class PieDataSet extends Dataset {
201 202
202 _legendWidget ??= legend == null 203 _legendWidget ??= legend == null
203 ? null 204 ? null
204 - : Text(legend!, style: legendStyle, textAlign: _legendAlign); 205 + : RichText(
  206 + text: TextSpan(
  207 + children: [TextSpan(text: legend!, style: legendStyle)],
  208 + style: TextStyle(
  209 + color: lp == PieLegendPosition.inside
  210 + ? color!.isLight
  211 + ? PdfColors.white
  212 + : PdfColors.black
  213 + : null),
  214 + ),
  215 + textAlign: _legendAlign,
  216 + );
205 217
206 if (_legendWidget != null) { 218 if (_legendWidget != null) {
207 _legendWidget!.layout(context, 219 _legendWidget!.layout(context,