| 1 |
// Package sixel provides sixel graphics format functionality. |
| 2 |
package sixel |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"image/color" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/lucasb-eyer/go-colorful" |
| 10 |
) |
| 11 |
|
| 12 |
// ErrInvalidColor is returned when a Sixel color is invalid. |
| 13 |
var ErrInvalidColor = fmt.Errorf("invalid color") |
| 14 |
|
| 15 |
// WriteColor writes a Sixel color to a writer. If pu is 0, the rest of the |
| 16 |
// parameters are ignored. |
| 17 |
func WriteColor(w io.Writer, pc, pu, px, py, pz int) (int, error) { |
| 18 |
if pu <= 0 || pu > 2 { |
| 19 |
return fmt.Fprintf(w, "#%d", pc) //nolint:wrapcheck |
| 20 |
} |
| 21 |
|
| 22 |
return fmt.Fprintf(w, "#%d;%d;%d;%d;%d", pc, pu, px, py, pz) //nolint:wrapcheck |
| 23 |
} |
| 24 |
|
| 25 |
// ConvertChannel converts a color channel from color.Color 0xffff to 0-100 |
| 26 |
// Sixel RGB format. |
| 27 |
func ConvertChannel(c uint32) uint32 { |
| 28 |
// We add 328 because that is about 0.5 in the sixel 0-100 color range, we're trying to |
| 29 |
// round to the nearest value |
| 30 |
return (c + 328) * 100 / 0xffff |
| 31 |
} |
| 32 |
|
| 33 |
// FromColor returns a Sixel color from a color.Color. It converts the color |
| 34 |
// channels to the 0-100 range. |
| 35 |
func FromColor(c color.Color) Color { |
| 36 |
if c == nil { |
| 37 |
return Color{} |
| 38 |
} |
| 39 |
|
| 40 |
r, g, b, _ := c.RGBA() |
| 41 |
return Color{ |
| 42 |
Pu: 2, // Always use RGB format "2" |
| 43 |
Px: int(ConvertChannel(r)), |
| 44 |
Py: int(ConvertChannel(g)), |
| 45 |
Pz: int(ConvertChannel(b)), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// DecodeColor decodes a Sixel color from a byte slice. It returns the Color and |
| 50 |
// the number of bytes read. |
| 51 |
func DecodeColor(data []byte) (c Color, n int) { |
| 52 |
if len(data) == 0 || data[0] != ColorIntroducer { |
| 53 |
return c, n |
| 54 |
} |
| 55 |
|
| 56 |
if len(data) < 2 { // The minimum length is 2: the introducer and a digit. |
| 57 |
return c, n |
| 58 |
} |
| 59 |
|
| 60 |
// Parse the color number and optional color system. |
| 61 |
pc := &c.Pc |
| 62 |
for n = 1; n < len(data); n++ { |
| 63 |
if data[n] == ';' { |
| 64 |
if pc == &c.Pc { |
| 65 |
pc = &c.Pu |
| 66 |
} else { |
| 67 |
n++ |
| 68 |
break |
| 69 |
} |
| 70 |
} else if data[n] >= '0' && data[n] <= '9' { |
| 71 |
*pc = (*pc)*10 + int(data[n]-'0') |
| 72 |
} else { |
| 73 |
break |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Parse the color components. |
| 78 |
ptr := &c.Px |
| 79 |
for ; n < len(data); n++ { |
| 80 |
if data[n] == ';' { //nolint:nestif |
| 81 |
if ptr == &c.Px { |
| 82 |
ptr = &c.Py |
| 83 |
} else if ptr == &c.Py { |
| 84 |
ptr = &c.Pz |
| 85 |
} else { |
| 86 |
n++ |
| 87 |
break |
| 88 |
} |
| 89 |
} else if data[n] >= '0' && data[n] <= '9' { |
| 90 |
*ptr = (*ptr)*10 + int(data[n]-'0') |
| 91 |
} else { |
| 92 |
break |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return c, n |
| 97 |
} |
| 98 |
|
| 99 |
// Color represents a Sixel color. |
| 100 |
type Color struct { |
| 101 |
// Pc is the color number (0-255). |
| 102 |
Pc int |
| 103 |
// Pu is an optional color system |
| 104 |
// - 0: default color map |
| 105 |
// - 1: HLS |
| 106 |
// - 2: RGB |
| 107 |
Pu int |
| 108 |
// Color components range from 0-100 for RGB values. For HLS format, the Px |
| 109 |
// (Hue) component ranges from 0-360 degrees while L (Lightness) and S |
| 110 |
// (Saturation) are 0-100. |
| 111 |
Px, Py, Pz int |
| 112 |
} |
| 113 |
|
| 114 |
// RGBA implements the color.Color interface. |
| 115 |
func (c Color) RGBA() (r, g, b, a uint32) { |
| 116 |
switch c.Pu { |
| 117 |
case 1: |
| 118 |
return sixelHLS(c.Px, c.Py, c.Pz).RGBA() |
| 119 |
case 2: |
| 120 |
return sixelRGB(c.Px, c.Py, c.Pz).RGBA() |
| 121 |
default: |
| 122 |
return colorPalette[c.Pc].RGBA() |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// #define PALVAL(n,a,m) (((n) * (a) + ((m) / 2)) / (m)) |
| 127 |
func palval(n, a, m int) int { |
| 128 |
return (n*a + m/2) / m |
| 129 |
} |
| 130 |
|
| 131 |
func sixelRGB(r, g, b int) color.Color { |
| 132 |
return color.NRGBA{uint8(palval(r, 0xff, 100)), uint8(palval(g, 0xff, 100)), uint8(palval(b, 0xff, 100)), 0xFF} //nolint:gosec |
| 133 |
} |
| 134 |
|
| 135 |
func sixelHLS(h, l, s int) color.Color { |
| 136 |
return colorful.Hsl(float64(h), float64(s)/100.0, float64(l)/100.0).Clamped() |
| 137 |
} |