1 module dgt.color; 2 3 import dgt.io; 4 5 import derelict.sdl2.sdl : SDL_Color; 6 7 /** 8 Represents a color with 4 floating-point values 9 10 The values are clamped between 0 and 1 11 */ 12 struct Color 13 { 14 ///The red component 15 float r = 0; 16 ///The green component 17 float g = 0; 18 ///The blue component 19 float b = 0; 20 ///The alpha component 21 float a = 1; 22 23 @nogc nothrow: 24 void print() const 25 { 26 dgt.io.print("Color(", r, ", ", g, ", ", b, ", ", a, ")"); 27 } 28 29 pure: 30 SDL_Color opCast() const 31 { 32 SDL_Color c = SDL_Color( 33 cast(ubyte)(255 * r), cast(ubyte)(255 * g), cast(ubyte)(255 * b), cast(ubyte)(255 * a) 34 ); 35 return c; 36 } 37 38 static immutable white = Color(1, 1, 1, 1); 39 static immutable black = Color(0, 0, 0, 1); 40 static immutable red = Color(1, 0, 0, 1); 41 static immutable orange = Color(1, 0.5, 0, 1); 42 static immutable yellow = Color(1, 1, 0, 1); 43 static immutable green = Color(0, 1, 0, 1); 44 static immutable cyan = Color(0, 1, 1, 1); 45 static immutable blue = Color(0, 0, 1, 1); 46 static immutable purple = Color(1, 0, 1, 1); 47 static immutable indigo = Color(0.5, 0, 1, 1); 48 } 49 50 51 52 unittest 53 { 54 import dgt.io : println; 55 println("Should print a color equivalent to white: ", Color.white); 56 SDL_Color orangeSDL = cast(SDL_Color)Color.orange; 57 assert(orangeSDL.r == 255 && orangeSDL.g == 127 && orangeSDL.b == 0 && orangeSDL.a == 255); 58 }