1 module daffodil.colorspace;
2 
3 import std.conv;
4 import std..string;
5 import std.algorithm;
6 
7 /**
8  * A group of functions describing the operations for a :d:struct:`Pixel`.
9  */
10 struct ColorSpace {
11     ///
12     void function(const real[], const real, real[]) opScalarMul;
13     ///
14     void function(const real[], const real[], real[]) opColorAdd;
15     ///
16     string function(const real[]) toString;
17 }
18 
19 /// Standard RGB color space
20 @property const(ColorSpace*) RGB() { return &RGBColorSpace; }
21 
22 private const RGBColorSpace = ColorSpace(
23     (const real[] self, const real other, real[] target) {
24         assert(self.length == target.length);
25 
26         foreach (index; 0..self.length) {
27             target[index] = cast(real)(self[index] * other);
28         }
29     },
30     (const real[] self, const real[] other, real[] target) {
31         assert(self.length == target.length);
32         assert(self.length == other.length);
33         foreach (index; 0..self.length) {
34             target[index] = cast(real)min(self[index] + other[index], real.max);
35         }
36     },
37     (const real[] self) {
38         auto values = self.map!(to!string);
39         return "(" ~ values.join(", ") ~ ")";
40     }
41 );