The OpenD Programming Language

1 /**
2 
3 This module mostly exist to have a bridge with the ggplotd code and the ggplotd.color code. That is because the ggplotd.color code might become part of phobos one day and I don't want to depend directly on ggplotd's implementation, so that it stays easy to switch to an alternative (phobos) based implementation.
4 
5 */
6 module ggplotd.colourspace;
7 
8 import chsx = std.experimental.color.hsx;
9 import crgb = std.experimental.color.rgb;
10 import cxyz = std.experimental.color.xyz;
11 import cspace = std.experimental.color.colorspace;
12 import cColor = std.experimental.color;
13 
14 /// HCY colourspace
15 alias HCY = chsx.HCY!double;
16 /// RGB colourspace
17 alias RGB = crgb.RGB!("rgb",double);
18 /// RGBA colourspace
19 alias RGBA = crgb.RGB!("rgba",double);
20 /// XYZ colourspace
21 alias XYZ = cxyz.XYZ!double;
22 
23 /// Convert to another colour space
24 alias toColourSpace = cspace.convertColor;
25 
26 /// Check whether it is a colour
27 alias isColour = cColor.isColor;
28 
29 import cairo = cairo.cairo;
30 
31 /// Convert to Cairo colour
32 cairo.RGBA toCairoRGBA(C)( in C from )
33 {
34     auto rgb = toColourSpace!(RGBA,C)( from );
35     return cairo.RGBA(
36         rgb.r,
37         rgb.g,
38         rgb.b,
39         rgb.a
40     );
41 }
42 
43 /// Convert from Cairo colour to specified type (template)
44 C fromCairoRGBA(C)( in cairo.RGBA crgb )
45 {
46     auto rgba = RGBA( crgb.red, crgb.green, crgb.blue, crgb.alpha );
47     return toColourSpace!C( rgba );
48 }
49 
50 /// Convert colour to a tuple holding the values
51 auto toTuple(T : HCY)( T colour )
52 {
53     import std.typecons : Tuple;
54     return Tuple!(double, double, double)( colour.h, colour.c, colour.y );
55 }
56 
57 /// Convert colour to a tuple holding the values
58 auto toTuple(T : XYZ)( T colour )
59 {
60     import std.typecons : Tuple;
61     return Tuple!(double, double, double)( colour.X, colour.Y, colour.Z );
62 }
63 
64 /// Convert colour to a tuple holding the values
65 auto toTuple(T)( T colour )
66 {
67     import std.typecons : Tuple;
68     import std.typecons : Nullable;
69     import std.traits : isInstanceOf;
70     static if (isInstanceOf!(Nullable, T))
71         return Tuple!(double, double, double)( colour.get().r, colour.get().g, colour.get().b );
72     else
73         return Tuple!(double, double, double)( colour.r, colour.g, colour.b );
74 }