1 module skia.SKColorTable; 2 3 import skia.Definitions; 4 import skia.SkiaApi; 5 import skia.SKObject; 6 import skia.SKColor; 7 import skia.SKPMColor; 8 import skia.Exceptions; 9 10 class SKColorTable : SKObject, ISKSkipObjectRegistration 11 { 12 const int MaxLength = 256; 13 14 this (void* x, bool owns) 15 { 16 super(x, owns); 17 } 18 19 this () 20 { 21 this (new SKPMColor[MaxLength]); 22 } 23 24 this (int count) 25 { 26 this (new SKPMColor[count]); 27 } 28 29 this (SKColor[] colors) 30 { 31 this (colors, cast(int)colors.length); 32 } 33 34 this (SKColor[] colors, int count) 35 { 36 this (SKPMColor.PreMultiply (colors), count); 37 } 38 39 this (SKPMColor[] colors) 40 { 41 this (colors, cast(int)colors.length); 42 } 43 44 this (SKPMColor[] colors, int count) 45 { 46 this (CreateNew (colors, count), true); 47 if (Handle is null) { 48 throw new InvalidOperationException ("Unable to create a new SKColorTable instance."); 49 } 50 } 51 52 public override void* Handle() 53 { 54 return null; 55 } 56 private static void* CreateNew (SKPMColor[] colors, int count) 57 { 58 SKPMColor* c = colors.ptr; 59 return SkiaApi.sk_colortable_new (cast(uint*)c, count); 60 } 61 62 protected override void Dispose (bool disposing) 63 { 64 return super.Dispose (disposing); 65 } 66 67 68 int Count() 69 { 70 return SkiaApi.sk_colortable_count (cast(sk_colortable_t*)Handle); 71 } 72 73 SKPMColor[] Colors() 74 { 75 auto count = Count; 76 auto pointer = ReadColors (); 77 78 if (count == 0 || pointer is null) { 79 return new SKPMColor[0]; 80 } 81 82 return PtrToStructureArray!(SKPMColor) (pointer, count); 83 } 84 85 SKColor[] UnPreMultipledColors() 86 { 87 return SKPMColor.UnPreMultiply (Colors); 88 } 89 90 // SKPMColor this[int index] 91 // { 92 // auto count = Count; 93 // auto pointer = ReadColors (); 94 95 // if (index < 0 || index >= count || pointer is null) { 96 // throw new ArgumentOutOfRangeException (index.stringof); 97 // } 98 99 // return PtrToStructure<SKPMColor> (pointer, index); 100 // } 101 102 // SKColor GetUnPreMultipliedColor (int index) 103 // { 104 // return SKPMColor.UnPreMultiply (this[index]); 105 // } 106 107 void* ReadColors () 108 { 109 uint* colors; 110 SkiaApi.sk_colortable_read_colors (cast(sk_colortable_t*)Handle, &colors); 111 return cast(void*)colors; 112 } 113 } 114