David PHAM-VAN

Add support for CMYK, HSL anf HSV colors

# 1.2.0
* Change license to Apache 2.0
* Improve PdfRect
* Add support for CMYK, HSL anf HSV colors
# 1.1.1
* Improve PdfPoint and PdfRect
... ...
... ... @@ -67,5 +67,219 @@ class PdfColor {
(((b * 255.0).round() & 0xff) << 0)) &
0xFFFFFFFF;
PdfColorCmyk toCmyk() {
return PdfColorCmyk.fromRgb(r, g, b, a);
}
PdfColorHsv toHsv() {
return PdfColorHsv.fromRgb(r, g, b, a);
}
PdfColorHsl toHsl() {
return PdfColorHsl.fromRgb(r, g, b, a);
}
static double _linearizeColorComponent(double component) {
if (component <= 0.03928) return component / 12.92;
return math.pow((component + 0.055) / 1.055, 2.4);
}
double get luminance {
final double R = _linearizeColorComponent(r);
final double G = _linearizeColorComponent(g);
final double B = _linearizeColorComponent(b);
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
}
String toString() => "$runtimeType($r, $g, $b, $a)";
}
class PdfColorCmyk extends PdfColor {
final double c;
final double m;
final double y;
final double k;
const PdfColorCmyk(this.c, this.m, this.y, this.k, [double a = 1.0])
: super(
(1.0 - c) * (1.0 - k),
(1.0 - m) * (1.0 - k),
(1.0 - y) * (1.0 - k),
);
const PdfColorCmyk.fromRgb(double r, double g, double b, [double a = 1.0])
: k = 1.0 - r > g ? r : g > b ? r > g ? r : g : b,
c = (1.0 - r - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
(1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
m = (1.0 - g - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
(1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
y = (1.0 - b - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)) /
(1.0 - (1.0 - r > g ? r : g > b ? r > g ? r : g : b)),
super(r, g, b, a);
@override
PdfColorCmyk toCmyk() {
return this;
}
String toString() => "$runtimeType($c, $m, $y, $k, $a)";
}
double _getHue(
double red, double green, double blue, double max, double delta) {
double hue;
if (max == 0.0) {
hue = 0.0;
} else if (max == red) {
hue = 60.0 * (((green - blue) / delta) % 6);
} else if (max == green) {
hue = 60.0 * (((blue - red) / delta) + 2);
} else if (max == blue) {
hue = 60.0 * (((red - green) / delta) + 4);
}
/// Set hue to 0.0 when red == green == blue.
hue = hue.isNaN ? 0.0 : hue;
return hue;
}
class PdfColorHsv extends PdfColor {
final double hue;
final double saturation;
final double value;
const PdfColorHsv._(this.hue, this.saturation, this.value, double red,
double green, double blue, double alpha)
: super(red, green, blue, alpha);
factory PdfColorHsv(double hue, double saturation, double value,
[double alpha = 1.0]) {
final double chroma = saturation * value;
final double secondary =
chroma * (1.0 - (((hue / 60.0) % 2.0) - 1.0).abs());
final double match = value - chroma;
double red;
double green;
double blue;
if (hue < 60.0) {
red = chroma;
green = secondary;
blue = 0.0;
} else if (hue < 120.0) {
red = secondary;
green = chroma;
blue = 0.0;
} else if (hue < 180.0) {
red = 0.0;
green = chroma;
blue = secondary;
} else if (hue < 240.0) {
red = 0.0;
green = secondary;
blue = chroma;
} else if (hue < 300.0) {
red = secondary;
green = 0.0;
blue = chroma;
} else {
red = chroma;
green = 0.0;
blue = secondary;
}
return PdfColorHsv._(hue, saturation, value, red + match, green + match,
blue + match, alpha);
}
factory PdfColorHsv.fromRgb(double red, double green, double blue,
[double alpha]) {
final double max = math.max(red, math.max(green, blue));
final double min = math.min(red, math.min(green, blue));
final double delta = max - min;
final hue = _getHue(red, green, blue, max, delta);
final double saturation = max == 0.0 ? 0.0 : delta / max;
return PdfColorHsv._(hue, saturation, max, red, green, blue, alpha);
}
@override
PdfColorHsv toHsv() {
return this;
}
String toString() => "$runtimeType($hue, $saturation, $value, $a)";
}
class PdfColorHsl extends PdfColor {
final double hue;
final double saturation;
final double lightness;
const PdfColorHsl._(this.hue, this.saturation, this.lightness, double alpha,
double red, double green, double blue)
: super(red, green, blue, alpha);
factory PdfColorHsl(double hue, double saturation, double lightness,
[double alpha]) {
final double chroma = (1.0 - (2.0 * lightness - 1.0).abs()) * saturation;
final double secondary =
chroma * (1.0 - (((hue / 60.0) % 2.0) - 1.0).abs());
final double match = lightness - chroma / 2.0;
double red;
double green;
double blue;
if (hue < 60.0) {
red = chroma;
green = secondary;
blue = 0.0;
} else if (hue < 120.0) {
red = secondary;
green = chroma;
blue = 0.0;
} else if (hue < 180.0) {
red = 0.0;
green = chroma;
blue = secondary;
} else if (hue < 240.0) {
red = 0.0;
green = secondary;
blue = chroma;
} else if (hue < 300.0) {
red = secondary;
green = 0.0;
blue = chroma;
} else {
red = chroma;
green = 0.0;
blue = secondary;
}
return PdfColorHsl._(hue, saturation, lightness, alpha, red + match,
green + match, blue + match);
}
factory PdfColorHsl.fromRgb(double red, double green, double blue,
[double alpha]) {
final double max = math.max(red, math.max(green, blue));
final double min = math.min(red, math.min(green, blue));
final double delta = max - min;
final double hue = _getHue(red, green, blue, max, delta);
final double lightness = (max + min) / 2.0;
// Saturation can exceed 1.0 with rounding errors, so clamp it.
final double saturation = lightness == 1.0
? 0.0
: (delta / (1.0 - (2.0 * lightness - 1.0).abs())).clamp(0.0, 1.0);
return PdfColorHsl._(hue, saturation, lightness, alpha, red, green, blue);
}
@override
PdfColorHsl toHsl() {
return this;
}
@override
String toString() => '$runtimeType($hue, $saturation, $lightness, $a)';
}
... ...
... ... @@ -182,10 +182,34 @@ class PdfGraphics {
///
/// @param c Color to use
void setColor(PdfColor color) {
buf.putNumList(<double>[color.r, color.g, color.b]);
buf.putString(" rg ");
buf.putNumList(<double>[color.r, color.g, color.b]);
buf.putString(" RG\n");
setFillColor(color);
setStrokeColor(color);
}
/// Sets the fill color for drawing
///
/// @param c Color to use
void setFillColor(PdfColor color) {
if (color is PdfColorCmyk) {
buf.putNumList(<double>[color.c, color.m, color.y, color.k]);
buf.putString(" k\n");
} else {
buf.putNumList(<double>[color.r, color.g, color.b]);
buf.putString(" rg\n");
}
}
/// Sets the stroke color for drawing
///
/// @param c Color to use
void setStrokeColor(PdfColor color) {
if (color is PdfColorCmyk) {
buf.putNumList(<double>[color.c, color.m, color.y, color.k]);
buf.putString(" K\n");
} else {
buf.putNumList(<double>[color.r, color.g, color.b]);
buf.putString(" RG\n");
}
}
/// Set the transformation Matrix
... ...