github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/pixel/cmc/image.go (about) 1 package main 2 3 import "image/color" 4 5 type HL struct{ H, L float32 } 6 7 type Image struct { 8 Width int 9 Height int 10 Pix []HL 11 } 12 13 func NewImage() *Image { 14 return &Image{} 15 } 16 17 func (m *Image) SetSize(w, h int) bool { 18 if m.Width != w || m.Height != h { 19 m.Width = w 20 m.Height = h 21 m.Pix = make([]HL, w*h) 22 return true 23 } 24 return false 25 } 26 27 func (m *Image) Clear() { 28 for i := range m.Pix { 29 m.Pix[i] = HL{} 30 } 31 } 32 33 func (m *Image) Put(v V, b HL) { 34 x, y := int(v.X), int(v.Y) 35 if x < 0 || m.Width <= x { 36 return 37 } 38 if y < 0 || m.Height <= y { 39 return 40 } 41 i := y*m.Width + x 42 m.Pix[i] = blendhl(m.Pix[i], b) 43 } 44 45 func (m *Image) Smear() { 46 si := 0 47 for y := 1; y < m.Height-1; y++ { 48 for x := 1; x < m.Width; x++ { 49 ti := si + x + m.Width 50 p := m.Pix[ti] 51 p.L *= 0.5 52 m.Pix[ti-m.Width] = blendhl(m.Pix[ti-m.Width], p) 53 m.Pix[ti-1] = blendhl(m.Pix[ti-1], p) 54 } 55 si += m.Width 56 } 57 } 58 59 func blendhl(a, b HL) HL { 60 return HL{ 61 H: (a.H*a.L + b.H*b.L) / (a.L + b.L), 62 L: a.L + b.L, 63 } 64 } 65 66 func (hl HL) RGBA(scale float32) color.RGBA { 67 r, g, b, _ := hsla(hl.H, 0.7, Clamp1(hl.L*scale), 1) 68 return color.RGBA{sat8(r), sat8(g), sat8(b), 0xFF} 69 }