diff options
| author | Mike Wey | 2011-02-20 20:41:35 +0100 |
|---|---|---|
| committer | Mike Wey | 2011-02-20 20:41:35 +0100 |
| commit | efd789405e397e0d3d3a3055b80b3350be7444e1 (patch) | |
| tree | b5dc0bedbd8ee9c2d1e5c965f6696dd5728ee45e /dmagick | |
| parent | ff26b979509498cc5ebd2ab003b6f3e530fdc4c9 (diff) | |
Add ColorRGB
Diffstat (limited to 'dmagick')
| -rw-r--r-- | dmagick/Color.d | 120 | ||||
| -rw-r--r-- | dmagick/ColorRGB.d | 109 | ||||
| -rw-r--r-- | dmagick/Options.d | 14 | ||||
| -rw-r--r-- | dmagick/c/quantum.d | 7 |
4 files changed, 231 insertions, 19 deletions
diff --git a/dmagick/Color.d b/dmagick/Color.d index e7fa690..19b1a94 100644 --- a/dmagick/Color.d +++ b/dmagick/Color.d @@ -6,6 +6,7 @@ module dmagick.Color; +import std.conv; import std.string; import dmagick.Exception; @@ -13,8 +14,9 @@ import dmagick.Utils; import dmagick.c.color; import dmagick.c.exception; -import dmagick.c.pixel; import dmagick.c.magickType; +import dmagick.c.pixel; +import dmagick.c.quantum; /** * A container for the pixel values: red, green, blue and opacity. @@ -34,15 +36,7 @@ class Color /** * Create a Color from the specified Quantums. */ - this(Quantum red, Quantum green, Quantum blue) - { - this(red, green, blue, 0); - } - - /** - * ditto - */ - this(Quantum red, Quantum green, Quantum blue, Quantum opacity) + this(Quantum red, Quantum green, Quantum blue, Quantum opacity = 0) { this(); @@ -97,9 +91,72 @@ class Color override string toString() { - //TODO + if ( pixelPacket.opacity == 0 ) + return format("#%04X%04X%04X", pixelPacket.red, pixelPacket.green, pixelPacket.blue); + else + return format("#%04X%04X%04X%04X", pixelPacket.red, pixelPacket.green, pixelPacket.blue, pixelPacket.opacity); + } + + void redQuantum(Quantum red) + { + pixelPacket.red = red; + } + Quantum redQuantum() + { + return pixelPacket.red; + } + + void greenQuantum(Quantum green) + { + pixelPacket.green = green; + } + Quantum greenQuantum() + { + return pixelPacket.green; + } - return "none"; + void blueQuantum(Quantum blue) + { + pixelPacket.blue = blue; + } + Quantum blueQuantum() + { + return pixelPacket.blue; + } + + void opacityByte(ubyte opacity) + { + pixelPacket.opacity = ScaleCharToQuantum(opacity); + } + ubyte opacityByte() + { + return ScaleQuantumToChar(pixelPacket.opacity); + } + + void opacityQuantum(Quantum opacity) + { + pixelPacket.opacity = opacity; + } + Quantum opacityQuantum() + { + return pixelPacket.opacity; + } + + void opacity(double opacity) + { + pixelPacket.opacity = scaleDoubleToQuantum(opacity); + } + double opacity() + { + return scaleQuantumToDouble(pixelPacket.opacity); + } + + double intensity() + { + //The Constants used here are derived from BT.709 Which standardizes HDTV + + return scaleQuantumToDouble(cast(Quantum)( + 0.2126*pixelPacket.red+0.7152*pixelPacket.green+0.0722*pixelPacket.blue)); } /** @@ -109,4 +166,43 @@ class Color { return new Color(*pixelPacket); } + + /** + * Returns the name of the color or the value as a hex string. + */ + string name() + { + char* pattern; + size_t numberOfColors; + const(ColorInfo)** colorList; + ExceptionInfo* exception = AcquireExceptionInfo(); + + copyString(pattern, "*"); + colorList = GetColorInfoList(pattern, &numberOfColors, exception); + DMagickException.throwException(exception); + + for ( int i = 0; i < numberOfColors; i++ ) + { + if ( colorList[i].compliance == ComplianceType.UndefinedCompliance ) + continue; + + MagickPixelPacket color = colorList[i].color; + + if ( pixelPacket.red == color.red && pixelPacket.green == color.green + && pixelPacket.blue == color.blue && pixelPacket.opacity == color.opacity ) + return to!(string)(colorList[i].name); + } + + return toString(); + } + + static Quantum scaleDoubleToQuantum(double value) + { + return cast(Quantum)(value*QuantumRange); + } + + static double scaleQuantumToDouble(Quantum value) + { + return (cast(double)value)/QuantumRange; + } } diff --git a/dmagick/ColorRGB.d b/dmagick/ColorRGB.d new file mode 100644 index 0000000..777c300 --- /dev/null +++ b/dmagick/ColorRGB.d @@ -0,0 +1,109 @@ +/** + * Copyright: Mike Wey 2011 + * License: To be determined + * Authors: Mike Wey + */ + +module dmagick.ColorRGB; + +import dmagick.Color; + +import dmagick.c.magickType; +import dmagick.c.quantum; + +class ColorRGB : Color +{ + this() + { + super(); + } + + /** + * Create a Color from the specified Bytes. + */ + this(ubyte red, ubyte green, ubyte blue, ubyte opacity = 0) + { + super(); + + pixelPacket.red = ScaleCharToQuantum(red); + pixelPacket.green = ScaleCharToQuantum(green); + pixelPacket.blue = ScaleCharToQuantum(blue); + pixelPacket.opacity = ScaleCharToQuantum(opacity); + } + + /** + * Create a Color from the specified doubles. + * The values should be between 0.0 and 1.0. + */ + this(double red, double green, double blue, double opacity = 0) + { + super(); + + pixelPacket.red = scaleDoubleToQuantum(red); + pixelPacket.green = scaleDoubleToQuantum(green); + pixelPacket.blue = scaleDoubleToQuantum(blue); + pixelPacket.opacity = scaleDoubleToQuantum(opacity); + } + + /** + * Create a Color from a X11 color specification string + */ + this(string color) + { + super(color); + } + + void redByte(ubyte red) + { + pixelPacket.red = ScaleCharToQuantum(red); + } + ubyte redByte() + { + return ScaleQuantumToChar(pixelPacket.red); + } + + void greenByte(ubyte green) + { + pixelPacket.green = ScaleCharToQuantum(green); + } + ubyte greenByte() + { + return ScaleQuantumToChar(pixelPacket.green); + } + + void blueByte(ubyte blue) + { + pixelPacket.blue = ScaleCharToQuantum(blue); + } + ubyte blueByte() + { + return ScaleQuantumToChar(pixelPacket.blue); + } + + void red(double red) + { + pixelPacket.red = scaleDoubleToQuantum(red); + } + double red() + { + return scaleQuantumToDouble(pixelPacket.red); + } + + void green(double green) + { + pixelPacket.green = scaleDoubleToQuantum(green); + } + double green() + { + return scaleQuantumToDouble(pixelPacket.green); + } + + void blue(double blue) + { + pixelPacket.blue = scaleDoubleToQuantum(blue); + } + double blue() + { + return scaleQuantumToDouble(pixelPacket.blue); + } +} diff --git a/dmagick/Options.d b/dmagick/Options.d index f7c931f..7e37042 100644 --- a/dmagick/Options.d +++ b/dmagick/Options.d @@ -94,7 +94,7 @@ class Options ///ditto void backgroundColor(Color color) { - imageInfo.background_color = color.pixelPacket; + imageInfo.background_color = *(color.pixelPacket); } ///ditto Color backgroundColor() @@ -127,8 +127,8 @@ class Options ///ditto void borderColor(Color color) { - imageInfo.border_color = color.pixelPacket; - drawInfo.border_color = color.pixelPacket; + imageInfo.border_color = *(color.pixelPacket); + drawInfo.border_color = *(color.pixelPacket); } ///ditto Color borderColor() @@ -351,7 +351,7 @@ class Options ///ditto void matteColor(Color color) { - imageInfo.matte_color = color.pixelPacket; + imageInfo.matte_color = *(color.pixelPacket); } ///ditto Color matteColor() @@ -742,7 +742,7 @@ class Options ///ditto void boxColor(Color color) { - drawInfo.undercolor = color.pixelPacket; + drawInfo.undercolor = *(color.pixelPacket); } ///ditto Color boxColor() @@ -761,7 +761,7 @@ class Options ///ditto void fillColor(Color color) { - drawInfo.fill = color.pixelPacket; + drawInfo.fill = *(color.pixelPacket); } ///ditto Color fillColor() @@ -822,7 +822,7 @@ class Options ///ditto void strokeColor(Color color) { - drawInfo.stroke = color.pixelPacket; + drawInfo.stroke = *(color.pixelPacket); } ///ditto Color strokeColor() diff --git a/dmagick/c/quantum.d b/dmagick/c/quantum.d index bd774af..4186c11 100644 --- a/dmagick/c/quantum.d +++ b/dmagick/c/quantum.d @@ -78,6 +78,13 @@ extern(C) return(cast(ubyte) (((quantum+128UL)-((quantum+128UL) >> 8)) >> 8)); } + static pure nothrow Quantum ScaleCharToQuantum(ubyte value) + { + enum Quantum factor = QuantumRange/255; + + return cast(Quantum)(factor*value); + } + MagickBooleanType SetQuantumDepth(const(Image)*, QuantumInfo*, const size_t); MagickBooleanType SetQuantumFormat(const(Image)*, QuantumInfo*, const QuantumFormatType); MagickBooleanType SetQuantumPad(const(Image)*, QuantumInfo*, const size_t); |
