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/Color.d | |
| parent | ff26b979509498cc5ebd2ab003b6f3e530fdc4c9 (diff) | |
Add ColorRGB
Diffstat (limited to 'dmagick/Color.d')
| -rw-r--r-- | dmagick/Color.d | 120 |
1 files changed, 108 insertions, 12 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; + } } |
