9fans.net/go@v0.0.5/draw/color_test.go (about) 1 package draw 2 3 import ( 4 "image/color" 5 "testing" 6 ) 7 8 type AtTest struct { 9 im *Image 10 p Point 11 r, g, b, a uint8 12 } 13 14 var ( 15 r1 = Rect(0, 0, 1, 1) 16 r2 = Rect(1, 1, 2, 2) 17 p0 = Pt(0, 0) 18 p1 = Pt(101, 101) // Far outside source of the replicated box. 19 top = Color(0x884422FF) 20 bot = Color(0x773311FF) 21 gry = Color(0x30303030) 22 AA = Color(0xAAAAAAAA) 23 AB = Color(0xABABABAB) 24 DD = Color(0xDDDDDDDD) 25 ) 26 27 var atTests = []AtTest{ 28 // GREY1 29 {alloc(r1, GREY1, true, Black), p0, 0x00, 0x00, 0x00, 0xFF}, 30 {alloc(r1, GREY1, true, White), p0, 0xFF, 0xFF, 0xFF, 0xFF}, 31 // GREY2 32 {alloc(r1, GREY2, true, Black), p0, 0x00, 0x00, 0x00, 0xFF}, 33 {alloc(r1, GREY2, true, White), p0, 0xFF, 0xFF, 0xFF, 0xFF}, 34 {alloc(r1, GREY2, true, AA), p0, 0xAA, 0xAA, 0xAA, 0xFF}, 35 {alloc(r2, GREY2, true, AA), p1, 0xAA, 0xAA, 0xAA, 0xFF}, 36 // GREY4 37 {alloc(r1, GREY4, true, Black), p0, 0x00, 0x00, 0x00, 0xFF}, 38 {alloc(r1, GREY4, true, White), p0, 0xFF, 0xFF, 0xFF, 0xFF}, 39 {alloc(r1, GREY4, true, DD), p0, 0xDD, 0xDD, 0xDD, 0xFF}, 40 {alloc(r2, GREY4, true, AA), p1, 0xAA, 0xAA, 0xAA, 0xFF}, 41 // GREY8 42 {alloc(r1, GREY8, true, Black), p0, 0x00, 0x00, 0x00, 0xFF}, 43 {alloc(r1, GREY8, true, White), p0, 0xFF, 0xFF, 0xFF, 0xFF}, 44 {alloc(r1, GREY8, true, AB), p0, 0xAB, 0xAB, 0xAB, 0xFF}, 45 {alloc(r1, GREY8, true, AA), p0, 0xAA, 0xAA, 0xAA, 0xFF}, 46 {alloc(r2, GREY8, true, AB), p1, 0xAB, 0xAB, 0xAB, 0xFF}, 47 // CMAP8 Cannot represent all 8-bit values accurately. 48 {alloc(r1, CMAP8, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 49 {alloc(r1, CMAP8, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 50 {alloc(r1, CMAP8, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 51 {alloc(r1, CMAP8, true, top), p0, 0x88, 0x44, 0x00, 0xFF}, 52 {alloc(r1, CMAP8, true, bot), p0, 0x88, 0x44, 0x00, 0xFF}, 53 {alloc(r1, CMAP8, true, gry), p0, 0x33, 0x33, 0x33, 0xFF}, 54 {alloc(r2, CMAP8, true, gry), p1, 0x33, 0x33, 0x33, 0xFF}, 55 // RGB15 Cannot represent all 8-bit values accurately. 56 {alloc(r1, RGB15, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 57 {alloc(r1, RGB15, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 58 {alloc(r1, RGB15, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 59 {alloc(r1, RGB15, true, top), p0, 0x8C, 0x42, 0x21, 0xFF}, 60 {alloc(r1, RGB15, true, bot), p0, 0x73, 0x31, 0x10, 0xFF}, 61 {alloc(r1, RGB15, true, gry), p0, 0x31, 0x31, 0x31, 0xFF}, 62 {alloc(r2, RGB15, true, top), p1, 0x8C, 0x42, 0x21, 0xFF}, 63 // RGB16 Cannot represent all 8-bit values accurately. 64 {alloc(r1, RGB16, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 65 {alloc(r1, RGB16, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 66 {alloc(r1, RGB16, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 67 {alloc(r1, RGB16, true, top), p0, 0x8C, 0x45, 0x21, 0xFF}, 68 {alloc(r1, RGB16, true, bot), p0, 0x73, 0x30, 0x10, 0xFF}, 69 {alloc(r1, RGB16, true, gry), p0, 0x31, 0x30, 0x31, 0xFF}, 70 {alloc(r2, RGB16, true, top), p1, 0x8C, 0x45, 0x21, 0xFF}, 71 // RGB24 72 {alloc(r1, RGB24, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 73 {alloc(r1, RGB24, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 74 {alloc(r1, RGB24, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 75 {alloc(r1, RGB24, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 76 {alloc(r1, RGB24, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 77 {alloc(r1, RGB24, true, gry), p0, 0x30, 0x30, 0x30, 0xFF}, 78 {alloc(r2, RGB24, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 79 // BGR24 80 {alloc(r1, BGR24, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 81 {alloc(r1, BGR24, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 82 {alloc(r1, BGR24, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 83 {alloc(r1, BGR24, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 84 {alloc(r1, BGR24, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 85 {alloc(r1, BGR24, true, gry), p0, 0x30, 0x30, 0x30, 0xFF}, 86 {alloc(r2, BGR24, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 87 // RGBA32 88 {alloc(r1, RGBA32, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 89 {alloc(r1, RGBA32, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 90 {alloc(r1, RGBA32, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 91 {alloc(r1, RGBA32, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 92 {alloc(r1, RGBA32, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 93 {alloc(r1, RGBA32, true, gry), p0, 0x30, 0x30, 0x30, 0x30}, 94 {alloc(r2, RGBA32, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 95 // ARGB32 96 {alloc(r1, ARGB32, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 97 {alloc(r1, ARGB32, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 98 {alloc(r1, ARGB32, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 99 {alloc(r1, ARGB32, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 100 {alloc(r1, ARGB32, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 101 {alloc(r1, ARGB32, true, gry), p0, 0x30, 0x30, 0x30, 0x30}, 102 {alloc(r2, ARGB32, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 103 // ABGR32 104 {alloc(r1, ABGR32, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 105 {alloc(r1, ABGR32, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 106 {alloc(r1, ABGR32, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 107 {alloc(r1, ABGR32, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 108 {alloc(r1, ABGR32, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 109 {alloc(r1, ABGR32, true, gry), p0, 0x30, 0x30, 0x30, 0x30}, 110 {alloc(r2, ABGR32, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 111 // XRGB32 112 {alloc(r1, XRGB32, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 113 {alloc(r1, XRGB32, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 114 {alloc(r1, XRGB32, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 115 {alloc(r1, XRGB32, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 116 {alloc(r1, XRGB32, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 117 {alloc(r1, XRGB32, true, gry), p0, 0x30, 0x30, 0x30, 0xFF}, 118 {alloc(r2, XRGB32, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 119 // XBGR32 120 {alloc(r1, XBGR32, true, Red), p0, 0xFF, 0x00, 0x00, 0xFF}, 121 {alloc(r1, XBGR32, true, Green), p0, 0x00, 0xFF, 0x00, 0xFF}, 122 {alloc(r1, XBGR32, true, Blue), p0, 0x00, 0x00, 0xFF, 0xFF}, 123 {alloc(r1, XBGR32, true, top), p0, 0x88, 0x44, 0x22, 0xFF}, 124 {alloc(r1, XBGR32, true, bot), p0, 0x77, 0x33, 0x11, 0xFF}, 125 {alloc(r1, XBGR32, true, gry), p0, 0x30, 0x30, 0x30, 0xFF}, 126 {alloc(r2, XBGR32, true, top), p1, 0x88, 0x44, 0x22, 0xFF}, 127 } 128 129 func alloc(r Rectangle, pix Pix, repl bool, color Color) *Image { 130 i, err := display().AllocImage(r, pix, repl, color) 131 if err != nil { 132 panic(err) 133 } 134 return i 135 } 136 137 func TestAt(t *testing.T) { 138 for i, test := range atTests { 139 r, g, b, a := test.im.At(test.p.X, test.p.Y).RGBA() 140 got := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} 141 want := color.RGBA{test.r, test.g, test.b, test.a} 142 if got != want { 143 t.Errorf("%d: got %x want %x", i, got, want) 144 } 145 } 146 } 147 148 func TestAtGrey1(t *testing.T) { 149 col := func(x int) Color { 150 c := Color(0x000000FF) 151 if x&1 != 0 { 152 c = Color(0xFFFFFFFF) 153 } 154 return c 155 } 156 i := alloc(Rect(0, 0, 4, 1), GREY1, false, 0) 157 for x := 0; x < 4; x++ { 158 bit := alloc(r1, GREY2, false, col(x)) 159 i.Draw(Rect(x, 0, x+1, 1), bit, nil, p0) 160 } 161 for x := 0; x < 4; x++ { 162 r, g, b, a := i.At(x, 0).RGBA() 163 got := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} 164 want := color.RGBA{0, 0, 0, 0xFF} 165 if x&1 != 0 { 166 want = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} 167 } 168 if got != want { 169 t.Errorf("%d: got %x want %x", x, got, want) 170 } 171 } 172 } 173 174 // Functions to create a pixel value that depends on x. 175 // The value is replicated so the same setting works for GREY2 and GREY4. 176 // For example, if x == 1, the pixel bits are 01010101, or 0x55. 177 func val(x int) int { 178 c := x 179 c |= c << 2 180 return c | c<<4 181 } 182 183 func col(x int) Color { 184 value := val(x) 185 return Color(value<<24 | value<<16 | value<<8 | 0xFF) 186 } 187 188 func rgba(x int) color.Color { 189 val := uint8(val(x)) 190 return color.RGBA{val, val, val, 0xFF} 191 } 192 193 func TestAtGrey2(t *testing.T) { 194 i := alloc(Rect(0, 0, 4, 1), GREY2, false, 0) 195 for x := 0; x < 4; x++ { 196 bit := alloc(r1, GREY2, false, col(x)) 197 i.Draw(Rect(x, 0, x+1, 1), bit, nil, p0) 198 } 199 for x := 0; x < 4; x++ { 200 r, g, b, a := i.At(x, 0).RGBA() 201 got := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} 202 want := rgba(x) 203 if got != want { 204 t.Errorf("%d: got %x want %x", x, got, want) 205 } 206 } 207 } 208 209 func TestAtGrey4(t *testing.T) { 210 i := alloc(Rect(0, 0, 4, 1), GREY4, false, 0) 211 for x := 0; x < 4; x++ { 212 bit := alloc(r1, GREY4, false, col(x)) 213 i.Draw(Rect(x, 0, x+1, 1), bit, nil, p0) 214 } 215 for x := 0; x < 4; x++ { 216 r, g, b, a := i.At(x, 0).RGBA() 217 got := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} 218 want := rgba(x) 219 if got != want { 220 t.Errorf("%d: got %x want %x", x, got, want) 221 } 222 } 223 }