You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

505 lines
12 KiB

  1. //go:generate go run color_table_gen.go
  2. package pixelcolor
  3. import (
  4. "image/color"
  5. )
  6. // Color models supported by this package.
  7. var (
  8. BitModel = color.ModelFunc(bitModel)
  9. RGB332Model = color.ModelFunc(rgb332Model)
  10. RGB565Model = color.ModelFunc(rgb565Model)
  11. RGB888Model = color.ModelFunc(rgb888Model)
  12. RGBA2222Model = color.ModelFunc(rgba2222Model)
  13. RGBA3323Model = color.ModelFunc(rgba3323Model)
  14. RGBA4444Model = color.ModelFunc(rgba4444Model)
  15. RGBA5551Model = color.ModelFunc(rgba5551Model)
  16. RGBX5551Model = color.ModelFunc(rgbx5551Model)
  17. RGBA5656Model = color.ModelFunc(rgba5656Model)
  18. RGBA8888Model = color.RGBAModel
  19. RGBA1010102Model = color.ModelFunc(rgba1010102Model)
  20. ARGB1555Model = color.ModelFunc(argb1555Model)
  21. ARGB2222Model = color.ModelFunc(argb2222Model)
  22. ARGB3332Model = color.ModelFunc(argb3332Model)
  23. ARGB4444Model = color.ModelFunc(argb4444Model)
  24. ARGB6565Model = color.ModelFunc(argb6565Model)
  25. ARGB8888Model = color.ModelFunc(argb8888Model)
  26. ARGB2101010Model = color.ModelFunc(argb2101010Model)
  27. XRGB1555Model = color.ModelFunc(xrgb1555Model)
  28. )
  29. func bitModel(c color.Color) color.Color { return ToBit(c) }
  30. func rgb332Model(c color.Color) color.Color { return ToRGB332(c) }
  31. func rgb565Model(c color.Color) color.Color { return ToRGB565(c) }
  32. func rgb888Model(c color.Color) color.Color { return ToRGB888(c) }
  33. func rgba2222Model(c color.Color) color.Color { return ToRGBA2222(c) }
  34. func rgba3323Model(c color.Color) color.Color { return ToRGBA3323(c) }
  35. func rgba4444Model(c color.Color) color.Color { return ToRGBA4444(c) }
  36. func rgba5551Model(c color.Color) color.Color { return ToRGBA5551(c) }
  37. func rgbx5551Model(c color.Color) color.Color { return ToRGBX5551(c) }
  38. func rgba5656Model(c color.Color) color.Color { return ToRGBA5656(c) }
  39. func rgba1010102Model(c color.Color) color.Color { return ToRGBA1010102(c) }
  40. func argb1555Model(c color.Color) color.Color { return ToARGB1555(c) }
  41. func argb2222Model(c color.Color) color.Color { return ToARGB2222(c) }
  42. func argb3332Model(c color.Color) color.Color { return ToARGB3332(c) }
  43. func argb4444Model(c color.Color) color.Color { return ToARGB4444(c) }
  44. func argb6565Model(c color.Color) color.Color { return ToARGB6565(c) }
  45. func argb8888Model(c color.Color) color.Color { return ToARGB8888(c) }
  46. func argb2101010Model(c color.Color) color.Color { return ToARGB2101010(c) }
  47. func xrgb1555Model(c color.Color) color.Color { return ToXRGB1555(c) }
  48. // Bit is a 1-bit color.
  49. type Bit bool
  50. // Bit values.
  51. const (
  52. Off Bit = false
  53. On Bit = true
  54. )
  55. func (c Bit) RGBA() (r, g, b, a uint32) {
  56. if c {
  57. return 0xffff, 0xffff, 0xffff, 0xffff
  58. }
  59. return 0, 0, 0, 0xffff
  60. }
  61. func ToBit(c color.Color) Bit {
  62. switch c := c.(type) {
  63. case Bit:
  64. return c
  65. default:
  66. r, g, b, _ := c.RGBA()
  67. // These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
  68. // as those given by the JFIF specification and used by func RGBToYCbCr in
  69. // ycbcr.go.
  70. //
  71. // Note that 19595 + 38470 + 7471 equals 65536.
  72. y := (19595*r + 38470*g + 7471*b + 1<<15) >> 24
  73. return y >= 0x80
  74. }
  75. }
  76. // RGB332 is a 8-bit RGB color with no alpha channel.
  77. type RGB332 uint8 // 3-3-2 RGB
  78. func (c RGB332) RGBA() (r, g, b, a uint32) {
  79. r = lut3to8[(c&0b111_000_00)>>5]
  80. g = lut3to8[(c&0b000_111_00)>>2]
  81. b = lut2to8[(c&0b000_000_11)>>0]
  82. a = 0xffff
  83. return
  84. }
  85. func ToRGB332(c color.Color) RGB332 {
  86. if c, ok := c.(RGB332); ok {
  87. return c
  88. }
  89. r, g, b, _ := c.RGBA()
  90. r = lut8to3[(r>>8)&0xff]
  91. g = lut8to3[(g>>8)&0xff]
  92. b = lut8to2[(b>>8)&0xff]
  93. return RGB332(r<<5 | g<<2 | b)
  94. }
  95. // RGB565 is a 16-bit RGB color with no alpha channel.
  96. type RGB565 uint16 // 5-6-5 RGB
  97. func (c RGB565) RGBA() (r, g, b, a uint32) {
  98. r = lut5to8[(c&0b11111_000000_00000)>>11]
  99. g = lut6to8[(c&0b00000_111111_00000)>>5]
  100. b = lut5to8[(c&0b00000_000000_11111)>>0]
  101. a = 0xffff
  102. return
  103. }
  104. func ToRGB565(c color.Color) RGB565 {
  105. r, g, b, _ := c.RGBA()
  106. r = lut8to5[(r>>8)&0xff]
  107. g = lut8to6[(g>>8)&0xff]
  108. b = lut8to5[(b>>8)&0xff]
  109. return RGB565(r<<11 | g<<5 | b)
  110. }
  111. // RGB888 is a 24-bit color with no alpha channel.
  112. type RGB888 struct {
  113. R, G, B uint8
  114. }
  115. func (c RGB888) RGBA() (r, g, b, a uint32) {
  116. r = uint32(c.R)
  117. r |= r << 8
  118. g = uint32(c.G)
  119. g |= g << 8
  120. b = uint32(c.B)
  121. b |= b << 8
  122. a = 0xffff
  123. return
  124. }
  125. func ToRGB888(c color.Color) RGB888 {
  126. r, g, b, _ := c.RGBA()
  127. return RGB888{byte(r), byte(g), byte(b)}
  128. }
  129. // RGBA2222 is a 8-bit RGB color with alpha channel.
  130. type RGBA2222 uint8
  131. func (c RGBA2222) RGBA() (r, g, b, a uint32) {
  132. r = lut2to8[uint32(c&0b11_00_00_00)>>6]
  133. g = lut2to8[uint32(c&0b00_11_00_00)>>4]
  134. b = lut2to8[uint32(c&0b00_00_11_00)>>2]
  135. a = lut2to8[uint32(c&0b00_00_00_11)>>0]
  136. return
  137. }
  138. func ToRGBA2222(c color.Color) RGBA2222 {
  139. if c, ok := c.(RGBA2222); ok {
  140. return c
  141. }
  142. r, g, b, a := c.RGBA()
  143. r = lut8to2[(r>>8)&0xff]
  144. g = lut8to2[(g>>8)&0xff]
  145. b = lut8to2[(b>>8)&0xff]
  146. a = lut8to2[(a>>8)&0xff]
  147. return RGBA2222(r<<6 | g<<4 | b<<2 | a)
  148. }
  149. // RGBA3323 is a 8-bit RGB color with alpha channel.
  150. type RGBA3323 uint16
  151. func (c RGBA3323) RGBA() (r, g, b, a uint32) {
  152. r = lut3to8[(c&0b111_000_00_000)>>8]
  153. g = lut3to8[(c&0b000_111_00_000)>>5]
  154. b = lut2to8[(c&0b000_000_11_000)>>3]
  155. a = lut3to8[(c&0b000_000_00_111)>>0]
  156. return
  157. }
  158. func ToRGBA3323(c color.Color) RGBA3323 {
  159. if c, ok := c.(RGBA3323); ok {
  160. return c
  161. }
  162. r, g, b, a := c.RGBA()
  163. r = lut8to3[(r>>8)&0xff]
  164. g = lut8to3[(g>>8)&0xff]
  165. b = lut8to2[(b>>8)&0xff]
  166. a = lut8to3[(a>>8)&0xff]
  167. return RGBA3323(r<<8 | g<<5 | b<<3 | a)
  168. }
  169. // RGBA4444 is a 16-bit RGB color with alpha channel.
  170. type RGBA4444 uint16
  171. func (c RGBA4444) RGBA() (r, g, b, a uint32) {
  172. r = lut4to8[(c&0b1111_0000_0000_0000)>>12]
  173. g = lut4to8[(c&0b0000_1111_0000_0000)>>8]
  174. b = lut4to8[(c&0b0000_0000_1111_0000)>>4]
  175. a = lut4to8[(c&0b0000_0000_0000_1111)>>0]
  176. return
  177. }
  178. func ToRGBA4444(c color.Color) RGBA4444 {
  179. if c, ok := c.(RGBA4444); ok {
  180. return c
  181. }
  182. r, g, b, a := c.RGBA()
  183. r = lut8to4[(r>>8)&0xff]
  184. g = lut8to4[(g>>8)&0xff]
  185. b = lut8to4[(b>>8)&0xff]
  186. a = lut8to4[(a>>8)&0xff]
  187. return RGBA4444(r<<12 | g<<8 | b<<4 | a)
  188. }
  189. // RGBA5551 is a 16-bit RGB color with alpha channel.
  190. type RGBA5551 uint16
  191. func (c RGBA5551) RGBA() (r, g, b, a uint32) {
  192. r = lut5to8[(c&0b11111_00000_00000_0)>>11]
  193. g = lut5to8[(c&0b00000_11111_00000_0)>>6]
  194. b = lut5to8[(c&0b00000_00000_11111_0)>>1]
  195. if (c & 0b00000_00000_00000_1) == 1 {
  196. a = 0xffff
  197. }
  198. return
  199. }
  200. func ToRGBA5551(c color.Color) RGBA5551 {
  201. if c, ok := c.(RGBA5551); ok {
  202. return c
  203. }
  204. r, g, b, a := c.RGBA()
  205. r = lut8to5[(r>>8)&0xff]
  206. g = lut8to5[(g>>8)&0xff]
  207. b = lut8to5[(b>>8)&0xff]
  208. if a > 0 {
  209. a = 1
  210. }
  211. return RGBA5551(r<<11 | g<<6 | b<<1 | a)
  212. }
  213. // RGBX5551 is a 16-bit RGB color without alpha channel.
  214. type RGBX5551 uint16
  215. func (c RGBX5551) RGBA() (r, g, b, a uint32) {
  216. r = lut5to8[(c&0b11111_00000_00000_0)>>11]
  217. g = lut5to8[(c&0b00000_11111_00000_0)>>6]
  218. b = lut5to8[(c&0b00000_00000_11111_0)>>1]
  219. a = 0xffff
  220. return
  221. }
  222. func ToRGBX5551(c color.Color) RGBX5551 {
  223. if c, ok := c.(RGBX5551); ok {
  224. return c
  225. }
  226. r, g, b, _ := c.RGBA()
  227. r = lut8to5[(r>>8)&0xff]
  228. g = lut8to5[(g>>8)&0xff]
  229. b = lut8to5[(b>>8)&0xff]
  230. return RGBX5551(r<<11 | g<<6 | b<<1)
  231. }
  232. // RGBA5656 is a 22-bit RGB color with alpha channel.
  233. type RGBA5656 uint32
  234. func (c RGBA5656) RGBA() (r, g, b, a uint32) {
  235. r = lut5to8[(c&0b11111_000000_00000_000000)>>17]
  236. g = lut6to8[(c&0b00000_111111_00000_000000)>>11]
  237. b = lut5to8[(c&0b00000_000000_11111_000000)>>6]
  238. a = lut6to8[(c&0b00000_000000_00000_111111)>>0]
  239. return
  240. }
  241. func ToRGBA5656(c color.Color) RGBA5656 {
  242. if c, ok := c.(RGBA5656); ok {
  243. return c
  244. }
  245. r, g, b, a := c.RGBA()
  246. r = lut8to5[(r>>8)&0xff]
  247. g = lut8to6[(g>>8)&0xff]
  248. b = lut8to5[(b>>8)&0xff]
  249. a = lut8to6[(a>>8)&0xff]
  250. return RGBA5656(r<<17 | g<<11 | b<<6 | a)
  251. }
  252. // RGBA1010102 is a 32-bit RGB color with alpha channel.
  253. type RGBA1010102 uint32
  254. func (c RGBA1010102) RGBA() (r, g, b, a uint32) {
  255. r = uint32(c>>22) & 0x03ff
  256. r = r<<6 | r
  257. g = uint32(c>>12) & 0x03ff
  258. g = g<<6 | g
  259. b = uint32(c>>2) & 0x03ff
  260. b = b<<6 | b
  261. a = lut2to8[c&0x0003]
  262. return
  263. }
  264. func ToRGBA1010102(c color.Color) RGBA1010102 {
  265. if c, ok := c.(RGBA1010102); ok {
  266. return c
  267. }
  268. r, g, b, a := c.RGBA()
  269. r = (r >> 6) & 0x03ff
  270. g = (g >> 6) & 0x03ff
  271. b = (b >> 6) & 0x03ff
  272. a = (a >> 14) & 0x0003
  273. return RGBA1010102(r<<22 | g<<12 | b<<2 | a)
  274. }
  275. // ARGB1555 is a 16-bit RGB color with alpha channel.
  276. type ARGB1555 uint16
  277. func (c ARGB1555) RGBA() (r, g, b, a uint32) {
  278. if (c&0b1_00000_00000_00000)>>15 == 1 {
  279. a = 0xffff
  280. }
  281. r = lut5to8[(c&0b0_11111_00000_00000)>>10]
  282. g = lut5to8[(c&0b0_00000_11111_00000)>>5]
  283. b = lut5to8[(c&0b0_00000_00000_11111)>>0]
  284. return
  285. }
  286. func ToARGB1555(c color.Color) ARGB1555 {
  287. if c, ok := c.(ARGB1555); ok {
  288. return c
  289. }
  290. r, g, b, a := c.RGBA()
  291. r = lut8to5[(r>>8)&0xff]
  292. g = lut8to5[(g>>8)&0xff]
  293. b = lut8to5[(b>>8)&0xff]
  294. if a > 0 {
  295. a = 1
  296. }
  297. return ARGB1555(a<<15 | r<<10 | g<<5 | b)
  298. }
  299. // ARGB2222 is a 8-bit RGB color with alpha channel.
  300. type ARGB2222 uint8
  301. func (c ARGB2222) RGBA() (r, g, b, a uint32) {
  302. a = lut2to8[uint32(c&0b11_00_00_00)>>6]
  303. r = lut2to8[uint32(c&0b00_11_00_00)>>4]
  304. g = lut2to8[uint32(c&0b00_00_11_00)>>2]
  305. b = lut2to8[uint32(c&0b00_00_00_11)>>0]
  306. return
  307. }
  308. func ToARGB2222(c color.Color) ARGB2222 {
  309. if c, ok := c.(ARGB2222); ok {
  310. return c
  311. }
  312. r, g, b, a := c.RGBA()
  313. a = lut8to2[(a>>8)&0xff]
  314. r = lut8to2[(r>>8)&0xff]
  315. g = lut8to2[(g>>8)&0xff]
  316. b = lut8to2[(b>>8)&0xff]
  317. return ARGB2222(a<<6 | r<<4 | g<<2 | b)
  318. }
  319. // ARGB3332 is a 11-bit RGB color with alpha channel.
  320. type ARGB3332 uint16
  321. func (c ARGB3332) RGBA() (r, g, b, a uint32) {
  322. a = lut3to8[uint32(c&0b111_000_000_00)>>8]
  323. r = lut3to8[uint32(c&0b000_111_000_00)>>5]
  324. g = lut3to8[uint32(c&0b000_000_111_00)>>2]
  325. b = lut2to8[uint32(c&0b000_000_000_11)>>0]
  326. return
  327. }
  328. func ToARGB3332(c color.Color) ARGB3332 {
  329. if c, ok := c.(ARGB3332); ok {
  330. return c
  331. }
  332. r, g, b, a := c.RGBA()
  333. a = lut8to3[(a>>8)&0xff]
  334. r = lut8to3[(r>>8)&0xff]
  335. g = lut8to3[(g>>8)&0xff]
  336. b = lut8to2[(b>>8)&0xff]
  337. return ARGB3332(a<<8 | r<<5 | g<<2 | b)
  338. }
  339. // ARGB4444 is a 16-bit RGB color with alpha channel.
  340. type ARGB4444 uint16
  341. func (c ARGB4444) RGBA() (r, g, b, a uint32) {
  342. a = lut4to8[(c&0b1111_0000_0000_0000)>>12]
  343. r = lut4to8[(c&0b0000_1111_0000_0000)>>8]
  344. g = lut4to8[(c&0b0000_0000_1111_0000)>>4]
  345. b = lut4to8[(c&0b0000_0000_0000_1111)>>0]
  346. return
  347. }
  348. func ToARGB4444(c color.Color) ARGB4444 {
  349. if c, ok := c.(ARGB4444); ok {
  350. return c
  351. }
  352. r, g, b, a := c.RGBA()
  353. a = lut8to4[(a>>8)&0xff]
  354. r = lut8to4[(r>>8)&0xff]
  355. g = lut8to4[(g>>8)&0xff]
  356. b = lut8to4[(b>>8)&0xff]
  357. return ARGB4444(a<<12 | r<<8 | g<<4 | b)
  358. }
  359. // ARGB6565 is a 22-bit RGB color with alpha channel.
  360. type ARGB6565 uint32
  361. func (c ARGB6565) RGBA() (r, g, b, a uint32) {
  362. a = lut6to8[(c&0b111111_00000_000000_00000)>>17]
  363. r = lut5to8[(c&0b000000_11111_000000_00000)>>11]
  364. g = lut6to8[(c&0b000000_00000_111111_00000)>>5]
  365. b = lut5to8[(c&0b000000_00000_000000_11111)>>0]
  366. return
  367. }
  368. func ToARGB6565(c color.Color) ARGB6565 {
  369. if c, ok := c.(ARGB6565); ok {
  370. return c
  371. }
  372. r, g, b, a := c.RGBA()
  373. a = lut8to6[(a>>8)&0xff]
  374. r = lut8to5[(r>>8)&0xff]
  375. g = lut8to6[(g>>8)&0xff]
  376. b = lut8to5[(b>>8)&0xff]
  377. return ARGB6565(a<<17 | r<<11 | g<<5 | b)
  378. }
  379. // ARGB8888 is a 32-bit RGB color with alpha channel.
  380. type ARGB8888 uint32
  381. func (c ARGB8888) RGBA() (r, g, b, a uint32) {
  382. a = uint32(c&0xf000) >> 12
  383. r = uint32(c&0x0f00) >> 8
  384. g = uint32(c&0x00f0) >> 4
  385. b = uint32(c&0x000f) >> 0
  386. return
  387. }
  388. func ToARGB8888(c color.Color) ARGB8888 {
  389. if c, ok := c.(ARGB8888); ok {
  390. return c
  391. }
  392. r, g, b, a := c.RGBA()
  393. r >>= 8
  394. g >>= 8
  395. b >>= 8
  396. a >>= 8
  397. return ARGB8888((a&0xff)<<12 | (r&0xff)<<8 | (g&0xff)<<4 | (b & 0xff))
  398. }
  399. // ARGB2101010 is a 32-bit RGB color with alpha channel.
  400. type ARGB2101010 uint32
  401. func (c ARGB2101010) RGBA() (r, g, b, a uint32) {
  402. a = lut2to8[uint32(c>>30)&0x0003]
  403. r = uint32(c>>20) & 0x03ff
  404. r = r<<6 | r
  405. g = uint32(c>>10) & 0x03ff
  406. g = g<<6 | g
  407. b = uint32(c>>0) & 0x03ff
  408. b = b<<6 | b
  409. return
  410. }
  411. func ToARGB2101010(c color.Color) ARGB2101010 {
  412. if c, ok := c.(ARGB2101010); ok {
  413. return c
  414. }
  415. r, g, b, a := c.RGBA()
  416. a = (a >> 14) & 0x0003
  417. r = (r >> 6) & 0x03ff
  418. g = (g >> 6) & 0x03ff
  419. b = (b >> 6) & 0x03ff
  420. return ARGB2101010(a<<30 | r<<20 | g<<10 | b)
  421. }
  422. // XRGB1555 is a 16-bit RGB color without alpha channel.
  423. type XRGB1555 uint16
  424. func (c XRGB1555) RGBA() (r, g, b, a uint32) {
  425. r = lut5to8[(c&0b0_11111_00000_00000)>>10]
  426. g = lut5to8[(c&0b0_00000_11111_00000)>>5]
  427. b = lut5to8[(c&0b0_00000_00000_11111)>>0]
  428. a = 0xffff
  429. return
  430. }
  431. func ToXRGB1555(c color.Color) color.Color {
  432. if c, ok := c.(XRGB1555); ok {
  433. return c
  434. }
  435. r, g, b, _ := c.RGBA()
  436. r = lut8to5[(r>>8)&0xff]
  437. g = lut8to5[(g>>8)&0xff]
  438. b = lut8to5[(b>>8)&0xff]
  439. return XRGB1555(r<<10 | g<<5 | b)
  440. }