1 module CPUblit.colorspaces;
2 
3 /**
4  * CPUblit
5  * Color space descriptions by Laszlo Szeremi
6  */
7 
8 import std.bitmanip;
9 
10 union Pixel16Bit{
11 	ushort base;
12 	@nogc struct ColorSpaceRGB565{
13 		mixin(bitfields!(
14 			ubyte, "red", 5,
15 			ubyte, "green", 6,
16 			ubyte, "blue", 5));
17 	}
18 	@nogc struct ColorSpaceRGBA5551{
19 		mixin(bitfields!(
20 			ubyte, "red", 5,
21 			ubyte, "green", 5,
22 			ubyte, "blue", 5,
23 			ubyte, "alpha", 1));
24 	}
25 	@nogc struct ColorSpaceRGBA4444{
26 		mixin(bitfields!(
27 			ubyte, "red", 4,
28 			ubyte, "green", 4,
29 			ubyte, "blue", 4,
30 			ubyte, "alpha", 4));
31 	}
32 }
33 
34 union Pixel32Bit{
35 	uint base;
36 	/*@nogc struct ColorSpaceRGBA8888{
37 		ubyte[4] colors;	///Normal representation, aliases are used for color naming.
38 		version(LittleEndian){
39 			public @property @nogc ubyte alpha(){ return colors[3]; }
40 			public @property @nogc ubyte red(){ return colors[0]; }
41 			public @property @nogc ubyte green(){ return colors[1]; }
42 			public @property @nogc ubyte blue(){ return colors[2]; }
43 			public @property @nogc ubyte alpha(ubyte value){ return colors[3] = value; }
44 			public @property @nogc ubyte red(ubyte value){ return colors[0] = value; }
45 			public @property @nogc ubyte green(ubyte value){ return colors[1] = value; }
46 			public @property @nogc ubyte blue(ubyte value){ return colors[2] = value; }
47 		}else{
48 			public @property @nogc ubyte alpha(){ return colors[0]; }
49 			public @property @nogc ubyte red(){ return colors[3]; }
50 			public @property @nogc ubyte green(){ return colors[2]; }
51 			public @property @nogc ubyte blue(){ return colors[1]; }
52 			public @property @nogc ubyte alpha(ubyte value){ return colors[0] = value; }
53 			public @property @nogc ubyte red(ubyte value){ return colors[3] = value; }
54 			public @property @nogc ubyte green(ubyte value){ return colors[2] = value; }
55 			public @property @nogc ubyte blue(ubyte value){ return colors[1] = value; }
56 		}
57 		@nogc this(ubyte red, ubyte green, ubyte blue, ubyte alpha){
58 			this.red = red;
59 			this.green = green;
60 			this.blue = blue;
61 			this.alpha = alpha;
62 		}
63 	}*/
64 	@nogc struct ColorSpaceARGB{
65 		ubyte[4] colors;	///Normal representation, aliases are used for color naming.
66 		version(LittleEndian){
67 			public @property @nogc ubyte alpha(){ return colors[0]; }
68 			public @property @nogc ubyte red(){ return colors[1]; }
69 			public @property @nogc ubyte green(){ return colors[2]; }
70 			public @property @nogc ubyte blue(){ return colors[3]; }
71 			public @property @nogc ubyte alpha(ubyte value){ return colors[0] = value; }
72 			public @property @nogc ubyte red(ubyte value){ return colors[1] = value; }
73 			public @property @nogc ubyte green(ubyte value){ return colors[2] = value; }
74 			public @property @nogc ubyte blue(ubyte value){ return colors[3] = value; }
75 		}else{
76 			public @property @nogc ubyte alpha(){ return colors[3]; }
77 			public @property @nogc ubyte red(){ return colors[2]; }
78 			public @property @nogc ubyte green(){ return colors[1]; }
79 			public @property @nogc ubyte blue(){ return colors[0]; }
80 			public @property @nogc ubyte alpha(ubyte value){ return colors[3] = value; }
81 			public @property @nogc ubyte red(ubyte value){ return colors[2] = value; }
82 			public @property @nogc ubyte green(ubyte value){ return colors[1] = value; }
83 			public @property @nogc ubyte blue(ubyte value){ return colors[0] = value; }
84 		}
85 		@nogc this(ubyte red, ubyte green, ubyte blue, ubyte alpha){
86 			this.red = red;
87 			this.green = green;
88 			this.blue = blue;
89 			this.alpha = alpha;
90 		}
91 	}
92 	@nogc struct AlphaMask{
93 		ubyte[4] mask;	///Normal representation, aliases are used for color naming.
94 		public @property ubyte value(){
95 			return mask[0];
96 		}
97 		public @property ubyte value(ubyte val){
98 			mask[0] = value;
99 			mask[1] = value;
100 			mask[2] = value;
101 			mask[3] = value;
102 			return val;
103 		}
104 		@nogc this(ubyte value){
105 			mask[0] = value;
106 			mask[1] = value;
107 			mask[2] = value;
108 			mask[3] = value;
109 		}
110 	}
111 }
112 
113 public @nogc Pixel32Bit convCS(string colorspace)(Pixel16Bit input){
114 	static if(colorspace == "RGB565"){
115 		return Pixel32Bit.ColorSpaceARGB8888(input.ColorSpaceRGB565.red<<3 | input.ColorSpaceRGB565.red>>2, input.ColorSpaceRGB565.green<<2 | input.ColorSpaceRGB565.green>>6,
116 						input.ColorSpaceRGB565.blue<<3 | input.ColorSpaceRGB565.blue>>2, 255);
117 	}else static if(colorspace == "RGBA5551"){
118 		return Pixel32Bit.ColorSpaceARGB8888(input.ColorSpaceRGBA5551.red<<3 | input.ColorSpaceRGB565.red>>2, input.ColorSpaceRGBA5551.green<<3 | input.ColorSpaceRGB565.green>>2, 
119 						input.ColorSpaceRGBA5551.blue<<3 | input.ColorSpaceRGB565.blue>>2, input.ColorSpaceRGBA5551.alpha ? 255 : 0);
120 	}else static if(colorspace == "RGBA4444"){
121 		return Pixel32Bit.ColorSpaceARGB8888(input.ColorSpaceRGBA4444.red<<4 | input.ColorSpaceRGBA4444.red, input.ColorSpaceRGBA4444.green<<4 | input.ColorSpaceRGBA4444.green,
122 						input.ColorSpaceRGBA4444.blue<<4 | input.ColorSpaceRGBA4444.blue, input.ColorSpaceRGBA4444.alpha<<4 | input.ColorSpaceRGBA4444.alpha);
123 	}else static assert("Colorspace " ~ colorspace ~ " is not supported!");
124 }