Browse Source

More color formats

master v0.1.4
maze 10 months ago
parent
commit
7fa5c9398e
5 changed files with 804 additions and 152 deletions
  1. +383
    -151
      pixelcolor/color.go
  2. +257
    -0
      pixelcolor/color_table.go
  3. +159
    -0
      pixelcolor/color_table_gen.go
  4. +4
    -1
      pixelcolor/color_test.go
  5. +1
    -0
      pixelterm/term.go

+ 383
- 151
pixelcolor/color.go View File

@ -1,3 +1,5 @@
//go:generate go run color_table_gen.go
package pixelcolor
import (
@ -6,84 +8,47 @@ import (
// Color models supported by this package.
var (
BitModel = color.ModelFunc(bitModel)
RGB332Model = color.ModelFunc(rgb332Model)
RGB565Model = color.ModelFunc(rgb565Model)
RGB888Model = color.ModelFunc(rgb888Model)
RGBA4444Model = color.ModelFunc(rgba4444Model)
RGBA5551Model = color.ModelFunc(rgba5551Model)
BitModel = color.ModelFunc(bitModel)
RGB332Model = color.ModelFunc(rgb332Model)
RGB565Model = color.ModelFunc(rgb565Model)
RGB888Model = color.ModelFunc(rgb888Model)
RGBA2222Model = color.ModelFunc(rgba2222Model)
RGBA3323Model = color.ModelFunc(rgba3323Model)
RGBA4444Model = color.ModelFunc(rgba4444Model)
RGBA5551Model = color.ModelFunc(rgba5551Model)
RGBX5551Model = color.ModelFunc(rgbx5551Model)
RGBA5656Model = color.ModelFunc(rgba5656Model)
RGBA8888Model = color.RGBAModel
RGBA1010102Model = color.ModelFunc(rgba1010102Model)
ARGB1555Model = color.ModelFunc(argb1555Model)
ARGB2222Model = color.ModelFunc(argb2222Model)
ARGB3332Model = color.ModelFunc(argb3332Model)
ARGB4444Model = color.ModelFunc(argb4444Model)
ARGB6565Model = color.ModelFunc(argb6565Model)
ARGB8888Model = color.ModelFunc(argb8888Model)
ARGB2101010Model = color.ModelFunc(argb2101010Model)
XRGB1555Model = color.ModelFunc(xrgb1555Model)
)
func bitModel(c color.Color) color.Color {
if _, ok := c.(Bit); ok {
return c
}
r, g, b, _ := c.RGBA()
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
y := (19595*r + 38470*g + 7471*b + 1<<15) >> 24
return Bit(uint8(y) >= 0x80)
//return Bit((r | g | b) >= 0x8000)
}
func rgb332Model(c color.Color) color.Color {
if _, ok := c.(RGB332); ok {
return c
}
r, g, b, _ := c.RGBA()
r >>= 13
g >>= 13
b >>= 14
return RGB332(r<<5 | g<<2 | b)
}
func rgb565Model(c color.Color) color.Color {
if _, ok := c.(RGB565); ok {
return c
}
r, g, b, _ := c.RGBA()
// RRRRRGGGGGGBBBBB
return RGB565((r & 0xf800) | ((g & 0xfc00) >> 5) | ((b & 0xf800) >> 11))
}
func rgb888Model(c color.Color) color.Color {
if _, ok := c.(RGB888); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB888{uint8(r), uint8(g), uint8(b)}
}
func rgba4444Model(c color.Color) color.Color {
if _, ok := c.(RGBA4444); ok {
return c
}
r, g, b, a := c.RGBA()
r >>= 12
g >>= 12
b >>= 12
a >>= 12
return RGBA4444(r<<12 | g<<8 | b<<4 | a)
}
func rgba5551Model(c color.Color) color.Color {
if _, ok := c.(RGBA5551); ok {
return c
}
r, g, b, a := c.RGBA()
r >>= 11
g >>= 11
b >>= 11
if a > 0 {
a = 1
}
return RGBA5551(r<<11 | g<<6 | b<<1 | a)
}
func bitModel(c color.Color) color.Color { return ToBit(c) }
func rgb332Model(c color.Color) color.Color { return ToRGB332(c) }
func rgb565Model(c color.Color) color.Color { return ToRGB565(c) }
func rgb888Model(c color.Color) color.Color { return ToRGB888(c) }
func rgba2222Model(c color.Color) color.Color { return ToRGBA2222(c) }
func rgba3323Model(c color.Color) color.Color { return ToRGBA3323(c) }
func rgba4444Model(c color.Color) color.Color { return ToRGBA4444(c) }
func rgba5551Model(c color.Color) color.Color { return ToRGBA5551(c) }
func rgbx5551Model(c color.Color) color.Color { return ToRGBX5551(c) }
func rgba5656Model(c color.Color) color.Color { return ToRGBA5656(c) }
func rgba1010102Model(c color.Color) color.Color { return ToRGBA1010102(c) }
func argb1555Model(c color.Color) color.Color { return ToARGB1555(c) }
func argb2222Model(c color.Color) color.Color { return ToARGB2222(c) }
func argb3332Model(c color.Color) color.Color { return ToARGB3332(c) }
func argb4444Model(c color.Color) color.Color { return ToARGB4444(c) }
func argb6565Model(c color.Color) color.Color { return ToARGB6565(c) }
func argb8888Model(c color.Color) color.Color { return ToARGB8888(c) }
func argb2101010Model(c color.Color) color.Color { return ToARGB2101010(c) }
func xrgb1555Model(c color.Color) color.Color { return ToXRGB1555(c) }
// Bit is a 1-bit color.
type Bit bool
@ -107,7 +72,15 @@ func ToBit(c color.Color) Bit {
return c
default:
r, g, b, _ := c.RGBA()
return (r | g | b) >= 0x8000
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
y := (19595*r + 38470*g + 7471*b + 1<<15) >> 24
return y >= 0x80
}
}
@ -115,18 +88,9 @@ func ToBit(c color.Color) Bit {
type RGB332 uint8 // 3-3-2 RGB
func (c RGB332) RGBA() (r, g, b, a uint32) {
r = uint32(c&0b111_000_00) >> 4 // ............rrr0
r |= r << 3 // .........rrrrrr0
r |= r << 6 // ......rrrrrrrrr0
r |= r << 12 // rrrrrrrrrrrrrrr0
g = uint32(c&0b000_111_00) >> 1 // ............ggg0
g |= g << 3 // .........gggggg0
g |= g << 6 // ......ggggggggg0
g |= g << 12 // ggggggggggggggg0
b = uint32(c&0b000_000_11) >> 0 // ..............bb
b |= b << 2 // ............bbbb
b |= b << 4 // ........bbbbbbbb
b |= b << 8 // bbbbbbbbbbbbbbbb
r = lut3to8[(c&0b111_000_00)>>5]
g = lut3to8[(c&0b000_111_00)>>2]
b = lut2to8[(c&0b000_000_11)>>0]
return
}
@ -135,9 +99,9 @@ func ToRGB332(c color.Color) RGB332 {
return c
}
r, g, b, _ := c.RGBA()
r >>= 13
g >>= 13
b >>= 14
r = lut8to3[(r>>8)&0xff]
g = lut8to3[(g>>8)&0xff]
b = lut8to2[(b>>8)&0xff]
return RGB332(r<<5 | g<<2 | b)
}
@ -145,37 +109,18 @@ func ToRGB332(c color.Color) RGB332 {
type RGB565 uint16 // 5-6-5 RGB
func (c RGB565) RGBA() (r, g, b, a uint32) {
// To convert a color channel from 5 or 6 bits back to 16 bits, the short
// bit pattern is duplicated to fill all 16 bits.
// For example the green channel in rgb565 is the middle 6 bits:
// 00000GGGGGG00000
//
// To create a 16 bit channel, these bits are or-ed together starting at the
// highest bit:
// GGGGGG0000000000 shifted << 5
// 000000GGGGGG0000 shifted >> 1
// 000000000000GGGG shifted >> 7
//
// These patterns map the minimum (all bits 0) and maximum (all bits 1)
// 5 and 6 bit channel values to the minimum and maximum 16 bit channel
// values.
//
// Alpha is always 100% opaque since this model does not support
// transparency.
rBits := uint32(c & 0b11111_000000_00000)
gBits := uint32(c & 0b00000_111111_00000)
bBits := uint32(c & 0b00000_000000_11111)
r = uint32(rBits | rBits>>5 | rBits>>10 | rBits>>15)
g = uint32(gBits<<5 | gBits>>1 | gBits>>7)
b = uint32(bBits<<11 | bBits<<6 | bBits<<1 | bBits>>4)
a = 0xffff
r = lut5to8[(c&0b11111_000000_00000)>>11]
g = lut6to8[(c&0b00000_111111_00000)>>5]
b = lut5to8[(c&0b00000_000000_11111)>>0]
return
}
func ToRGB565(c color.Color) RGB565 {
r, g, b, _ := c.RGBA()
// RRRRRGGGGGGBBBBB
return RGB565((r & 0xf800) + ((g & 0xfc00) >> 5) + ((b & 0xf800) >> 11))
r = lut8to5[(r>>8)&0xff]
g = lut8to6[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
return RGB565(r<<11 | g<<5 | b)
}
// RGB888 is a 24-bit color with no alpha channel.
@ -198,26 +143,60 @@ func ToRGB888(c color.Color) RGB888 {
return RGB888{byte(r), byte(g), byte(b)}
}
// RGBA2222 is a 8-bit RGB color with alpha channel.
type RGBA2222 uint8
func (c RGBA2222) RGBA() (r, g, b, a uint32) {
r = lut2to8[uint32(c&0b11_00_00_00)>>6]
g = lut2to8[uint32(c&0b00_11_00_00)>>4]
b = lut2to8[uint32(c&0b00_00_11_00)>>2]
a = lut2to8[uint32(c&0b00_00_00_11)>>0]
return
}
func ToRGBA2222(c color.Color) RGBA2222 {
if c, ok := c.(RGBA2222); ok {
return c
}
r, g, b, a := c.RGBA()
r = lut8to2[(r>>8)&0xff]
g = lut8to2[(g>>8)&0xff]
b = lut8to2[(b>>8)&0xff]
a = lut8to2[(a>>8)&0xff]
return RGBA2222(r<<6 | g<<4 | b<<2 | a)
}
// RGBA3323 is a 8-bit RGB color with alpha channel.
type RGBA3323 uint16
func (c RGBA3323) RGBA() (r, g, b, a uint32) {
r = lut3to8[(c&0b111_000_00_000)>>8]
g = lut3to8[(c&0b000_111_00_000)>>5]
b = lut2to8[(c&0b000_000_11_000)>>3]
a = lut3to8[(c&0b000_000_00_111)>>0]
return
}
func ToRGBA3323(c color.Color) RGBA3323 {
if c, ok := c.(RGBA3323); ok {
return c
}
r, g, b, a := c.RGBA()
r = lut8to3[(r>>8)&0xff]
g = lut8to3[(g>>8)&0xff]
b = lut8to2[(b>>8)&0xff]
a = lut8to3[(a>>8)&0xff]
return RGBA3323(r<<8 | g<<5 | b<<3 | a)
}
// RGBA4444 is a 16-bit RGB color with alpha channel.
type RGBA4444 uint16
func (c RGBA4444) RGBA() (r, g, b, a uint32) {
r = uint32(c&0b1111_0000_0000_0000) >> 12
r |= r << 4
r |= r << 8
r |= r << 12
g = uint32(c&0b0000_1111_0000_0000) >> 8
g |= g << 4
g |= g << 8
g |= g << 12
b = uint32(c&0b0000_0000_1111_0000) >> 4
b |= b << 4
b |= b << 8
b |= b << 12
a = uint32(c&0b0000_0000_0000_1111) >> 0
a |= a << 4
a |= a << 8
a |= a << 12
r = lut4to8[(c&0b1111_0000_0000_0000)>>12]
g = lut4to8[(c&0b0000_1111_0000_0000)>>8]
b = lut4to8[(c&0b0000_0000_1111_0000)>>4]
a = lut4to8[(c&0b0000_0000_0000_1111)>>0]
return
}
@ -226,10 +205,10 @@ func ToRGBA4444(c color.Color) RGBA4444 {
return c
}
r, g, b, a := c.RGBA()
r >>= 12
g >>= 12
b >>= 12
a >>= 12
r = lut8to4[(r>>8)&0xff]
g = lut8to4[(g>>8)&0xff]
b = lut8to4[(b>>8)&0xff]
a = lut8to4[(a>>8)&0xff]
return RGBA4444(r<<12 | g<<8 | b<<4 | a)
}
@ -237,16 +216,10 @@ func ToRGBA4444(c color.Color) RGBA4444 {
type RGBA5551 uint16
func (c RGBA5551) RGBA() (r, g, b, a uint32) {
var (
rBits = uint32(c&0b11111_00000_00000_0) >> 11
gBits = uint32(c&0b00000_11111_00000_0) >> 6
bBits = uint32(c&0b00000_00000_11111_0) >> 1
aBits = uint32(c&0b00000_00000_00000_1) >> 0
)
r = (rBits | rBits<<5 | rBits<<10) << 1
g = (gBits | gBits<<5 | gBits<<10) << 1
b = (bBits | bBits<<5 | bBits<<10) << 1
if aBits == 1 {
r = lut5to8[(c&0b11111_00000_00000_0)>>11]
g = lut5to8[(c&0b00000_11111_00000_0)>>6]
b = lut5to8[(c&0b00000_00000_11111_0)>>1]
if (c & 0b00000_00000_00000_1) == 1 {
a = 0xffff
}
return
@ -257,11 +230,270 @@ func ToRGBA5551(c color.Color) RGBA5551 {
return c
}
r, g, b, a := c.RGBA()
r >>= 11
g >>= 11
b >>= 11
r = lut8to5[(r>>8)&0xff]
g = lut8to5[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
if a > 0 {
a = 1
}
return RGBA5551(r<<11 | g<<6 | b<<1 | a)
}
// RGBX5551 is a 16-bit RGB color without alpha channel.
type RGBX5551 uint16
func (c RGBX5551) RGBA() (r, g, b, a uint32) {
r = lut5to8[(c&0b11111_00000_00000_0)>>11]
g = lut5to8[(c&0b00000_11111_00000_0)>>6]
b = lut5to8[(c&0b00000_00000_11111_0)>>1]
return
}
func ToRGBX5551(c color.Color) RGBX5551 {
if c, ok := c.(RGBX5551); ok {
return c
}
r, g, b, _ := c.RGBA()
r = lut8to5[(r>>8)&0xff]
g = lut8to5[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
return RGBX5551(r<<11 | g<<6 | b<<1)
}
// RGBA5656 is a 22-bit RGB color with alpha channel.
type RGBA5656 uint32
func (c RGBA5656) RGBA() (r, g, b, a uint32) {
r = lut5to8[(c&0b11111_000000_00000_000000)>>17]
g = lut6to8[(c&0b00000_111111_00000_000000)>>11]
b = lut5to8[(c&0b00000_000000_11111_000000)>>6]
a = lut6to8[(c&0b00000_000000_00000_111111)>>0]
return
}
func ToRGBA5656(c color.Color) RGBA5656 {
if c, ok := c.(RGBA5656); ok {
return c
}
r, g, b, a := c.RGBA()
r = lut8to5[(r>>8)&0xff]
g = lut8to6[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
a = lut8to6[(a>>8)&0xff]
return RGBA5656(r<<17 | g<<11 | b<<6 | a)
}
// RGBA1010102 is a 32-bit RGB color with alpha channel.
type RGBA1010102 uint32
func (c RGBA1010102) RGBA() (r, g, b, a uint32) {
r = uint32(c>>22) & 0x03ff
r = r<<6 | r
g = uint32(c>>12) & 0x03ff
g = g<<6 | g
b = uint32(c>>2) & 0x03ff
b = b<<6 | b
a = lut2to8[c&0x0003]
return
}
func ToRGBA1010102(c color.Color) RGBA1010102 {
if c, ok := c.(RGBA1010102); ok {
return c
}
r, g, b, a := c.RGBA()
r = (r >> 6) & 0x03ff
g = (g >> 6) & 0x03ff
b = (b >> 6) & 0x03ff
a = (a >> 14) & 0x0003
return RGBA1010102(r<<22 | g<<12 | b<<2 | a)
}
// ARGB1555 is a 16-bit RGB color with alpha channel.
type ARGB1555 uint16
func (c ARGB1555) RGBA() (r, g, b, a uint32) {
if (c&0b1_00000_00000_00000)>>15 == 1 {
a = 0xffff
}
r = lut5to8[(c&0b0_11111_00000_00000)>>10]
g = lut5to8[(c&0b0_00000_11111_00000)>>5]
b = lut5to8[(c&0b0_00000_00000_11111)>>0]
return
}
func ToARGB1555(c color.Color) ARGB1555 {
if c, ok := c.(ARGB1555); ok {
return c
}
r, g, b, a := c.RGBA()
r = lut8to5[(r>>8)&0xff]
g = lut8to5[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
if a > 0 {
a = 1
}
return ARGB1555(a<<15 | r<<10 | g<<5 | b)
}
// ARGB2222 is a 8-bit RGB color with alpha channel.
type ARGB2222 uint8
func (c ARGB2222) RGBA() (r, g, b, a uint32) {
a = lut2to8[uint32(c&0b11_00_00_00)>>6]
r = lut2to8[uint32(c&0b00_11_00_00)>>4]
g = lut2to8[uint32(c&0b00_00_11_00)>>2]
b = lut2to8[uint32(c&0b00_00_00_11)>>0]
return
}
func ToARGB2222(c color.Color) ARGB2222 {
if c, ok := c.(ARGB2222); ok {
return c
}
r, g, b, a := c.RGBA()
a = lut8to2[(a>>8)&0xff]
r = lut8to2[(r>>8)&0xff]
g = lut8to2[(g>>8)&0xff]
b = lut8to2[(b>>8)&0xff]
return ARGB2222(a<<6 | r<<4 | g<<2 | b)
}
// ARGB3332 is a 11-bit RGB color with alpha channel.
type ARGB3332 uint16
func (c ARGB3332) RGBA() (r, g, b, a uint32) {
a = lut3to8[uint32(c&0b111_000_000_00)>>8]
r = lut3to8[uint32(c&0b000_111_000_00)>>5]
g = lut3to8[uint32(c&0b000_000_111_00)>>2]
b = lut2to8[uint32(c&0b000_000_000_11)>>0]
return
}
func ToARGB3332(c color.Color) ARGB3332 {
if c, ok := c.(ARGB3332); ok {
return c
}
r, g, b, a := c.RGBA()
a = lut8to3[(a>>8)&0xff]
r = lut8to3[(r>>8)&0xff]
g = lut8to3[(g>>8)&0xff]
b = lut8to2[(b>>8)&0xff]
return ARGB3332(a<<8 | r<<5 | g<<2 | b)
}
// ARGB4444 is a 16-bit RGB color with alpha channel.
type ARGB4444 uint16
func (c ARGB4444) RGBA() (r, g, b, a uint32) {
a = lut4to8[(c&0b1111_0000_0000_0000)>>12]
r = lut4to8[(c&0b0000_1111_0000_0000)>>8]
g = lut4to8[(c&0b0000_0000_1111_0000)>>4]
b = lut4to8[(c&0b0000_0000_0000_1111)>>0]
return
}
func ToARGB4444(c color.Color) ARGB4444 {
if c, ok := c.(ARGB4444); ok {
return c
}
r, g, b, a := c.RGBA()
a = lut8to4[(a>>8)&0xff]
r = lut8to4[(r>>8)&0xff]
g = lut8to4[(g>>8)&0xff]
b = lut8to4[(b>>8)&0xff]
return ARGB4444(a<<12 | r<<8 | g<<4 | b)
}
// ARGB6565 is a 22-bit RGB color with alpha channel.
type ARGB6565 uint32
func (c ARGB6565) RGBA() (r, g, b, a uint32) {
a = lut6to8[(c&0b111111_00000_000000_00000)>>17]
r = lut5to8[(c&0b000000_11111_000000_00000)>>11]
g = lut6to8[(c&0b000000_00000_111111_00000)>>5]
b = lut5to8[(c&0b000000_00000_000000_11111)>>0]
return
}
func ToARGB6565(c color.Color) ARGB6565 {
if c, ok := c.(ARGB6565); ok {
return c
}
r, g, b, a := c.RGBA()
a = lut8to6[(a>>8)&0xff]
r = lut8to5[(r>>8)&0xff]
g = lut8to6[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
return ARGB6565(a<<17 | r<<11 | g<<5 | b)
}
// ARGB8888 is a 32-bit RGB color with alpha channel.
type ARGB8888 uint32
func (c ARGB8888) RGBA() (r, g, b, a uint32) {
a = uint32(c&0xf000) >> 12
r = uint32(c&0x0f00) >> 8
g = uint32(c&0x00f0) >> 4
b = uint32(c&0x000f) >> 0
return
}
func ToARGB8888(c color.Color) ARGB8888 {
if c, ok := c.(ARGB8888); ok {
return c
}
r, g, b, a := c.RGBA()
r >>= 8
g >>= 8
b >>= 8
a >>= 8
return ARGB8888((a&0xff)<<12 | (r&0xff)<<8 | (g&0xff)<<4 | (b & 0xff))
}
// ARGB2101010 is a 32-bit RGB color with alpha channel.
type ARGB2101010 uint32
func (c ARGB2101010) RGBA() (r, g, b, a uint32) {
a = lut2to8[uint32(c>>30)&0x0003]
r = uint32(c>>20) & 0x03ff
r = r<<6 | r
g = uint32(c>>10) & 0x03ff
g = g<<6 | g
b = uint32(c>>0) & 0x03ff
b = b<<6 | b
return
}
func ToARGB2101010(c color.Color) ARGB2101010 {
if c, ok := c.(ARGB2101010); ok {
return c
}
r, g, b, a := c.RGBA()
a = (a >> 14) & 0x0003
r = (r >> 6) & 0x03ff
g = (g >> 6) & 0x03ff
b = (b >> 6) & 0x03ff
return ARGB2101010(a<<30 | r<<20 | g<<10 | b)
}
// XRGB1555 is a 16-bit RGB color without alpha channel.
type XRGB1555 uint16
func (c XRGB1555) RGBA() (r, g, b, a uint32) {
r = lut5to8[(c&0b0_11111_00000_00000)>>10]
g = lut5to8[(c&0b0_00000_11111_00000)>>5]
b = lut5to8[(c&0b0_00000_00000_11111)>>0]
return
}
func ToXRGB1555(c color.Color) color.Color {
if c, ok := c.(XRGB1555); ok {
return c
}
r, g, b, _ := c.RGBA()
r = lut8to5[(r>>8)&0xff]
g = lut8to5[(g>>8)&0xff]
b = lut8to5[(b>>8)&0xff]
return XRGB1555(r<<10 | g<<5 | b)
}

+ 257
- 0
pixelcolor/color_table.go View File

@ -0,0 +1,257 @@
package pixelcolor
var (
// lut2to8 contains pre-multiplied values
lut2to8 = [4]uint32{
0x0000, 0x5555, 0xaaaa, 0xffff}
lut8to2 = [256]uint32{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
}
// lut3to8 contains pre-multiplied values
lut3to8 = [8]uint32{
0x0000, 0x2424, 0x4949, 0x6d6d, 0x9292, 0xb6b6, 0xdbdb, 0xffff,
}
lut8to3 = [256]uint32{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
}
// lut4to8 contains pre-multiplied values
lut4to8 = [16]uint32{
0x0000, 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777,
0x8888, 0x9999, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd, 0xeeee, 0xffff,
}
lut8to4 = [256]uint32{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x09, 0x09, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0x0a, 0x0a, 0x0a, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0e, 0x0e,
0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0f,
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
}
// lut5to8 contains pre-multiplied values
lut5to8 = [32]uint32{
0x0000, 0x0808, 0x1010, 0x1919, 0x2121, 0x2929, 0x3131, 0x3a3a,
0x4242, 0x4a4a, 0x5252, 0x5a5a, 0x6363, 0x6b6b, 0x7373, 0x7b7b,
0x8484, 0x8c8c, 0x9494, 0x9c9c, 0xa5a5, 0xadad, 0xb5b5, 0xbdbd,
0xc5c5, 0xcece, 0xd6d6, 0xdede, 0xe6e6, 0xefef, 0xf7f7, 0xffff,
}
lut8to5 = [256]uint32{
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09,
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0a,
0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0c,
0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
0x15, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
0x16, 0x16, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
0x17, 0x17, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a,
0x1a, 0x1a, 0x1b, 0x1b, 0x1b, 0x1b, 0x1b, 0x1b,
0x1b, 0x1b, 0x1b, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c,
0x1c, 0x1c, 0x1c, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d,
0x1d, 0x1d, 0x1d, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
0x1e, 0x1e, 0x1e, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
}
// lut6to8 contains pre-multiplied values
lut6to8 = [64]uint32{
0x0000, 0x0404, 0x0808, 0x0c0c, 0x1010, 0x1414, 0x1818, 0x1c1c,
0x2020, 0x2424, 0x2828, 0x2d2d, 0x3131, 0x3535, 0x3939, 0x3d3d,
0x4141, 0x4545, 0x4949, 0x4d4d, 0x5151, 0x5555, 0x5959, 0x5d5d,
0x6161, 0x6565, 0x6969, 0x6d6d, 0x7171, 0x7575, 0x7979, 0x7d7d,
0x8282, 0x8686, 0x8a8a, 0x8e8e, 0x9292, 0x9696, 0x9a9a, 0x9e9e,
0xa2a2, 0xa6a6, 0xaaaa, 0xaeae, 0xb2b2, 0xb6b6, 0xbaba, 0xbebe,
0xc2c2, 0xc6c6, 0xcaca, 0xcece, 0xd2d2, 0xd7d7, 0xdbdb, 0xdfdf,
0xe3e3, 0xe7e7, 0xebeb, 0xefef, 0xf3f3, 0xf7f7, 0xfbfb, 0xffff,
}
lut8to6 = [256]uint32{
0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x02,
0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04,
0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x06,
0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x08,
0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x09, 0x0a,
0x0a, 0x0a, 0x0a, 0x0b, 0x0b, 0x0b, 0x0b, 0x0c,
0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d, 0x0d, 0x0e,
0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x10,
0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, 0x12,
0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, 0x14,
0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x15,
0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
0x18, 0x18, 0x18, 0x18, 0x19, 0x19, 0x19, 0x19,
0x1a, 0x1a, 0x1a, 0x1a, 0x1b, 0x1b, 0x1b, 0x1b,
0x1c, 0x1c, 0x1c, 0x1c, 0x1d, 0x1d, 0x1d, 0x1d,
0x1e, 0x1e, 0x1e, 0x1e, 0x1f, 0x1f, 0x1f, 0x1f,
0x20, 0x20, 0x20, 0x20, 0x21, 0x21, 0x21, 0x21,
0x22, 0x22, 0x22, 0x22, 0x23, 0x23, 0x23, 0x23,
0x24, 0x24, 0x24, 0x24, 0x25, 0x25, 0x25, 0x25,
0x26, 0x26, 0x26, 0x26, 0x27, 0x27, 0x27, 0x27,
0x28, 0x28, 0x28, 0x28, 0x29, 0x29, 0x29, 0x29,
0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2b, 0x2b, 0x2b,
0x2b, 0x2c, 0x2c, 0x2c, 0x2c, 0x2d, 0x2d, 0x2d,
0x2d, 0x2e, 0x2e, 0x2e, 0x2e, 0x2f, 0x2f, 0x2f,
0x2f, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31,
0x31, 0x32, 0x32, 0x32, 0x32, 0x33, 0x33, 0x33,
0x33, 0x34, 0x34, 0x34, 0x34, 0x35, 0x35, 0x35,
0x35, 0x36, 0x36, 0x36, 0x36, 0x37, 0x37, 0x37,
0x37, 0x38, 0x38, 0x38, 0x38, 0x39, 0x39, 0x39,
0x39, 0x3a, 0x3a, 0x3a, 0x3a, 0x3b, 0x3b, 0x3b,
0x3b, 0x3c, 0x3c, 0x3c, 0x3c, 0x3d, 0x3d, 0x3d,
0x3d, 0x3e, 0x3e, 0x3e, 0x3e, 0x3f, 0x3f, 0x3f,
}
// lut7to8 contains pre-multiplied values
lut7to8 = [128]uint32{
0x0000, 0x0202, 0x0404, 0x0606, 0x0808, 0x0a0a, 0x0c0c, 0x0e0e,
0x1010, 0x1212, 0x1414, 0x1616, 0x1818, 0x1a1a, 0x1c1c, 0x1e1e,
0x2020, 0x2222, 0x2424, 0x2626, 0x2828, 0x2a2a, 0x2c2c, 0x2e2e,
0x3030, 0x3232, 0x3434, 0x3636, 0x3838, 0x3a3a, 0x3c3c, 0x3e3e,
0x4040, 0x4242, 0x4444, 0x4646, 0x4848, 0x4a4a, 0x4c4c, 0x4e4e,
0x5050, 0x5252, 0x5454, 0x5656, 0x5858, 0x5a5a, 0x5c5c, 0x5e5e,
0x6060, 0x6262, 0x6464, 0x6666, 0x6868, 0x6a6a, 0x6c6c, 0x6e6e,
0x7070, 0x7272, 0x7474, 0x7676, 0x7878, 0x7a7a, 0x7c7c, 0x7e7e,
0x8181, 0x8383, 0x8585, 0x8787, 0x8989, 0x8b8b, 0x8d8d, 0x8f8f,
0x9191, 0x9393, 0x9595, 0x9797, 0x9999, 0x9b9b, 0x9d9d, 0x9f9f,
0xa1a1, 0xa3a3, 0xa5a5, 0xa7a7, 0xa9a9, 0xabab, 0xadad, 0xafaf,
0xb1b1, 0xb3b3, 0xb5b5, 0xb7b7, 0xb9b9, 0xbbbb, 0xbdbd, 0xbfbf,
0xc1c1, 0xc3c3, 0xc5c5, 0xc7c7, 0xc9c9, 0xcbcb, 0xcdcd, 0xcfcf,
0xd1d1, 0xd3d3, 0xd5d5, 0xd7d7, 0xd9d9, 0xdbdb, 0xdddd, 0xdfdf,
0xe1e1, 0xe3e3, 0xe5e5, 0xe7e7, 0xe9e9, 0xebeb, 0xeded, 0xefef,
0xf1f1, 0xf3f3, 0xf5f5, 0xf7f7, 0xf9f9, 0xfbfb, 0xfdfd, 0xffff,
}
lut8to7 = [256]uint32{
0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03,
0x04, 0x04, 0x05, 0x05, 0x06, 0x06, 0x07, 0x07,
0x08, 0x08, 0x09, 0x09, 0x0a, 0x0a, 0x0b, 0x0b,
0x0c, 0x0c, 0x0d, 0x0d, 0x0e, 0x0e, 0x0f, 0x0f,
0x10, 0x10, 0x11, 0x11, 0x12, 0x12, 0x13, 0x13,
0x14, 0x14, 0x15, 0x15, 0x16, 0x16, 0x17, 0x17,
0x18, 0x18, 0x19, 0x19, 0x1a, 0x1a, 0x1b, 0x1b,
0x1c, 0x1c, 0x1d, 0x1d, 0x1e, 0x1e, 0x1f, 0x1f,
0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
0x28, 0x28, 0x29, 0x29, 0x2a, 0x2a, 0x2b, 0x2b,
0x2c, 0x2c, 0x2d, 0x2d, 0x2e, 0x2e, 0x2f, 0x2f,
0x30, 0x30, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33,
0x34, 0x34, 0x35, 0x35, 0x36, 0x36, 0x37, 0x37,
0x38, 0x38, 0x39, 0x39, 0x3a, 0x3a, 0x3b, 0x3b,
0x3c, 0x3c, 0x3d, 0x3d, 0x3e, 0x3e, 0x3f, 0x3f,
0x40, 0x40, 0x41, 0x41, 0x42, 0x42, 0x43, 0x43,
0x44, 0x44, 0x45, 0x45, 0x46, 0x46, 0x47, 0x47,
0x48, 0x48, 0x49, 0x49, 0x4a, 0x4a, 0x4b, 0x4b,
0x4c, 0x4c, 0x4d, 0x4d, 0x4e, 0x4e, 0x4f, 0x4f,
0x50, 0x50, 0x51, 0x51, 0x52, 0x52, 0x53, 0x53,
0x54, 0x54, 0x55, 0x55, 0x56, 0x56, 0x57, 0x57,
0x58, 0x58, 0x59, 0x59, 0x5a, 0x5a, 0x5b, 0x5b,
0x5c, 0x5c, 0x5d, 0x5d, 0x5e, 0x5e, 0x5f, 0x5f,
0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
0x64, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67,
0x68, 0x68, 0x69, 0x69, 0x6a, 0x6a, 0x6b, 0x6b,
0x6c, 0x6c, 0x6d, 0x6d, 0x6e, 0x6e, 0x6f, 0x6f,
0x70, 0x70, 0x71, 0x71, 0x72, 0x72, 0x73, 0x73,
0x74, 0x74, 0x75, 0x75, 0x76, 0x76, 0x77, 0x77,
0x78, 0x78, 0x79, 0x79, 0x7a, 0x7a, 0x7b, 0x7b,
0x7c, 0x7c, 0x7d, 0x7d, 0x7e, 0x7e, 0x7f, 0x7f,
}
)

+ 159
- 0
pixelcolor/color_table_gen.go View File

@ -0,0 +1,159 @@
// +build ignore
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"io"
"math"
"os"
)
func main() {
outputFile := flag.String("output", "color_table.go", "output file")
flag.Parse()
b := new(bytes.Buffer)
fmt.Fprintln(b, "package pixelcolor")
fmt.Fprintln(b)
fmt.Fprintln(b, "var (")
for bits := 2; bits < 8; bits++ {
generateLUT(b, bits)
}
/*
// 2 to 8 bits
fmt.Fprintln(b, "\t // lut2to8 contains pre-multiplied values")
fmt.Fprint(b, "\tlut2to8 = [4]uint32{")
for v := 0; v < 0b00000100; v++ {
x := (v * 255 / 3)
fmt.Fprintf(b, "%#04x,", x|x<<8)
}
fmt.Fprintln(b, "}")
fmt.Fprintln(b, "\tlut8to2 = [256]uint32{")
for v := 0; v < 0x100; v++ {
fmt.Fprintf(b, "%#02x,", (v*3)/255)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
// 3 to 8 bits
fmt.Fprintln(b, "\t // lut3to8 contains pre-multiplied values")
fmt.Fprintln(b, "\tlut3to8 = [8]uint32{")
for v := 0; v < 8; v++ {
x := (v * 255 / 7)
fmt.Fprintf(b, "%#04x,", x|x<<8)
}
fmt.Fprintln(b, "\n\t}")
fmt.Fprintln(b, "\tlut8to3 = [256]uint32{")
for v := 0; v < 0x100; v++ {
fmt.Fprintf(b, "%#02x,", (v*7)/255)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
// 5 to 8 bits
fmt.Fprintln(b, "\t // lut5to8 contains pre-multiplied values")
fmt.Fprintln(b, "\tlut5to8 = [32]uint32{")
for v := 0; v < 0b00100000; v++ {
// x := (v * 255 / 31)
// x := (v*527 + 23) >> 6
x := int(math.Floor(float64(v)*255/31 + 0.5))
fmt.Fprintf(b, "%#04x,", x|x<<8)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
fmt.Fprintln(b, "\tlut8to5 = [256]uint32{")
for v := 0; v < 0x100; v++ {
// x := (v*31)/255
// x := (v*249 + 1014) >> 11
x := int(math.Floor(float64(v)*31/255 + 0.5))
fmt.Fprintf(b, "%#02x,", x)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
// 6 to 8 bits
fmt.Fprintln(b, "\t // lut6to8 contains pre-multiplied values")
fmt.Fprintln(b, "\tlut6to8 = [64]uint32{")
for v := 0; v < 0b01000000; v++ {
// x := (v * 255 / 63)
x := (v*259 + 33) >> 6
fmt.Fprintf(b, "%#04x,", x|x<<8)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
fmt.Fprintln(b, "\tlut8to6 = [256]uint32{")
for v := 0; v < 0x100; v++ {
// x := (v*63)/255
x := (v*253 + 505) >> 10
fmt.Fprintf(b, "%#02x,", x)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(b)
}
}
fmt.Fprintln(b, "\t}")
*/
fmt.Fprintln(b, ")")
s, err := format.Source(b.Bytes())
if err != nil {
fatal(err)
}
f, err := os.Create(*outputFile)
if err != nil {
fatal("error opening", *outputFile+":", err)
}
defer f.Close()
if _, err = f.Write(s); err != nil {
fatal("error saving:", err)
}
}
func fatal(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}
func generateLUT(w io.Writer, bits int) {
max := 1 << bits
fmt.Fprintf(w, "\t // lut%dto8 contains pre-multiplied values\n", bits)
fmt.Fprintf(w, "\tlut%dto8 = [%d]uint32{\n", bits, max)
for v := 0; v < 1<<bits; v++ {
// x := (v * 255 / 31)
// x := (v*527 + 23) >> 6
x := int(math.Floor(float64(v)*255/float64(max-1) + 0.5))
fmt.Fprintf(w, "%#04x,", x|x<<8)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(w)
}
}
fmt.Fprintln(w, "\t}")
fmt.Fprintf(w, "\tlut8to%d = [256]uint32{\n", bits)
for v := 0; v < 0x100; v++ {
// x := (v*31)/255
// x := (v*249 + 1014) >> 11
x := int(math.Floor(float64(v)*float64(max-1)/255 + 0.5))
fmt.Fprintf(w, "%#02x,", x)
if v > 0 && (v%8) == 7 {
fmt.Fprintln(w)
}
}
fmt.Fprintln(w, "\t}")
}

+ 4
- 1
pixelcolor/color_test.go View File

@ -31,7 +31,7 @@ var (
}
)
func TestRGB323(t *testing.T) {
func TestRGB332(t *testing.T) {
tests := []struct {
Name string
Test RGB332
@ -49,6 +49,9 @@ func TestRGB323(t *testing.T) {
for _, test := range tests {
t.Run(test.Name, func(it *testing.T) {
testColorSink = color.RGBAModel.Convert(test.Test)
tr, tg, tb, _ := testColorSink.RGBA()
it.Logf("RGB332(%#08b) -> RGB(%02x%02x%02x), want RGB(%02x%02x%02x)", test.Test,
tr&0xff, tg&0xff, tb&0xff, test.Want.R, test.Want.G, test.Want.B)
})
}
}


+ 1
- 0
pixelterm/term.go View File

@ -0,0 +1 @@
package pixelterm

Loading…
Cancel
Save