1 module rip.color.rgbManager; 2 3 private { 4 import std.algorithm; 5 6 import rip.color.rgb; 7 } 8 9 class RGBManager { 10 RGBColor[][][] cache; 11 int cached; 12 bool fullInitialization; 13 14 this(bool fullInitialization = false) { 15 cache.length = 256; 16 17 this.fullInitialization = fullInitialization; 18 19 if(fullInitialization) 20 initizalizeFullSpace(); 21 } 22 23 void initizalizeFullSpace() { 24 foreach(ref _array; cache) { 25 _array.length = 256; 26 foreach(ref __array; _array) { 27 __array.length = 256; 28 } 29 } 30 } 31 32 private bool freeColorPlace(T, U, V)(T red, U green, V blue) { 33 if(!fullInitialization) { 34 if(cache[red].length == 0) 35 cache[red].length = 256; 36 37 if(cache[red][green].length == 0) 38 cache[red][green].length = 256; 39 } 40 41 if(cache[red][green][blue] !is null) 42 return false; 43 44 return true; 45 } 46 47 public RGBColor getColor(T, U, V)(T _red, U _green, V _blue) { 48 ubyte red = cast(ubyte) clamp(_red, 0, 255); 49 ubyte green = cast(ubyte) clamp(_green, 0, 255); 50 ubyte blue = cast(ubyte) clamp(_blue, 0, 255); 51 52 bool initialized = this.freeColorPlace(red, green, blue); 53 54 if(initialized) { 55 cache[red][green][blue] = new RGBColor(red, green, blue); 56 cached++; 57 } 58 59 return cache[red][green][blue]; 60 } 61 62 public RGBColor getColor(RGBColor color) { 63 ubyte red = color.red!ubyte; 64 ubyte green = color.green!ubyte; 65 ubyte blue = color.blue!ubyte; 66 67 return this.getColor(red, green, blue); 68 } 69 70 // public bool putColor(RGBColor color) { 71 // ubyte red = color.red!ubyte; 72 // ubyte green = color.green!ubyte; 73 // ubyte blue = color.blue!ubyte; 74 75 // if(freeColorPlace(red, green, blue)) { 76 // cache[red][green][blue] = new RGBColor(red, green, blue); 77 // return true; 78 // } 79 // else 80 // return false; 81 // } 82 83 public int getCached() { 84 return cached; 85 } 86 }