func main() { example := []color.NRGBA{ {255, 255, 255, 255}, {255, 255, 0, 255}, {255, 0, 255, 255}, {255, 0, 0, 255}, {0, 255, 255, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}, {0, 0, 0, 255}, {128, 128, 128, 255}, {128, 128, 0, 255}, {128, 0, 128, 255}, {128, 0, 0, 255}, {0, 128, 128, 255}, {0, 128, 0, 255}, {0, 0, 128, 255}, {0, 0, 0, 255}, } exampleInterface := make([]color.Color, 0, len(example)) for _, c := range example { exampleInterface = append(exampleInterface, color.Color(c)) } colorshow.DisplaySwatches(exampleInterface) }
// slice of *TextColor -> color.Palette func NewPalette(p []*TextColor) color.Palette { pal := make([]color.Color, len(p)) for i, r := range p { pal[i] = color.Color(r) } return pal }
func main() { flag.Parse() bg := color.Color(color.RGBA{0, 0, 0, 0}) // default background if *bgColor != "" { bg = toColor(*bgColor) } fg := toColor(*fgColor) r, err := os.Open(*inFile) if err != nil { log.Fatal(err) } defer r.Close() m, _, err := image.Decode(r) if err != nil { log.Fatal(err) } var ( stride, pix int ) switch in := m.(type) { case *image.RGBA: stride = in.Stride pix = len(in.Pix) case *image.NRGBA: stride = in.Stride pix = len(in.Pix) } out := &image.RGBA{Pix: make([]uint8, pix), Stride: stride, Rect: m.Bounds()} max := m.Bounds().Max for x := 0; x < max.X; x++ { for y := 0; y < max.Y; y++ { v := m.At(x, y) if !*dropTransparency && isTransparent(v) { continue } if isTransparent(v) || isBackground(v) { out.Set(x, y, bg) continue } out.Set(x, y, fg) } } w, _ := os.Create(*outFile) defer func() { if err := w.Close(); err != nil { log.Fatal(err) } }() png.Encode(w, out) }
func transform_Uniform(dst Image, dr, adr image.Rectangle, d2s *f64.Aff3, src *image.Uniform, sr image.Rectangle, bias image.Point, op Op) { switch dst := dst.(type) { case *image.RGBA: pr, pg, pb, pa := src.C.RGBA() pr8 := uint8(pr >> 8) pg8 := uint8(pg >> 8) pb8 := uint8(pb >> 8) pa8 := uint8(pa >> 8) for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 d := dst.PixOffset(dr.Min.X+adr.Min.X, dr.Min.Y+int(dy)) for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx, d = dx+1, d+4 { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } dst.Pix[d+0] = pr8 dst.Pix[d+1] = pg8 dst.Pix[d+2] = pb8 dst.Pix[d+3] = pa8 } } default: pr, pg, pb, pa := src.C.RGBA() dstColorRGBA64 := &color.RGBA64{ uint16(pr), uint16(pg), uint16(pb), uint16(pa), } dstColor := color.Color(dstColorRGBA64) for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } dst.Set(dr.Min.X+int(dx), dr.Min.Y+int(dy), dstColor) } } } }
func (t *TreePalette) ColorPalette() (p color.Palette) { if t == nil { return } var walk func(*TreePalette) walk = func(t *TreePalette) { if t.Type == TLeaf { p = append(p, color.Color(t.Color)) return } walk(t.Low) walk(t.High) } walk(t) return }
func toRGBAImage(m image.Image) *image.RGBA { b := m.Bounds() rgba := image.NewRGBA(b) dstColorRGBA64 := &color.RGBA64{} dstColor := color.Color(dstColorRGBA64) for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { pr, pg, pb, pa := m.At(x, y).RGBA() dstColorRGBA64.R = uint16(pr) dstColorRGBA64.G = uint16(pg) dstColorRGBA64.B = uint16(pb) dstColorRGBA64.A = uint16(pa) rgba.Set(x, y, dstColor) } } return rgba }
func (z *Rasterizer) rasterizeOpSrc(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point) { z.accumulateMask() out := color.RGBA64{} outc := color.Color(&out) for y, y1 := 0, r.Max.Y-r.Min.Y; y < y1; y++ { for x, x1 := 0, r.Max.X-r.Min.X; x < x1; x++ { sr, sg, sb, sa := src.At(sp.X+x, sp.Y+y).RGBA() ma := z.bufU32[y*z.size.X+x] // This algorithm comes from the standard library's image/draw // package. out.R = uint16(sr * ma / 0xffff) out.G = uint16(sg * ma / 0xffff) out.B = uint16(sb * ma / 0xffff) out.A = uint16(sa * ma / 0xffff) dst.Set(r.Min.X+x, r.Min.Y+y, outc) } } }
func adjustImage(m image.Image) image.Image { switch m := m.(type) { case *image.Gray, *image.RGBA: return m default: b := m.Bounds() rgba := image.NewRGBA(b) dstColorRGBA64 := &color.RGBA64{} dstColor := color.Color(dstColorRGBA64) for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { pr, pg, pb, pa := m.At(x, y).RGBA() dstColorRGBA64.R = uint16(pr) dstColorRGBA64.G = uint16(pg) dstColorRGBA64.B = uint16(pb) dstColorRGBA64.A = uint16(pa) rgba.Set(x, y, dstColor) } } return rgba } }
func transform_Uniform(dst Image, dr, adr image.Rectangle, d2s *f64.Aff3, src *image.Uniform, sr image.Rectangle, bias image.Point, op Op) { switch op { case Over: switch dst := dst.(type) { case *image.RGBA: pr, pg, pb, pa := src.C.RGBA() pa1 := (0xffff - pa) * 0x101 for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 d := dst.PixOffset(dr.Min.X+adr.Min.X, dr.Min.Y+int(dy)) for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx, d = dx+1, d+4 { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*pa1/0xffff + pr) >> 8) dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*pa1/0xffff + pg) >> 8) dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*pa1/0xffff + pb) >> 8) dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*pa1/0xffff + pa) >> 8) } } default: pr, pg, pb, pa := src.C.RGBA() pa1 := 0xffff - pa dstColorRGBA64 := &color.RGBA64{} dstColor := color.Color(dstColorRGBA64) for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } qr, qg, qb, qa := dst.At(dr.Min.X+int(dx), dr.Min.Y+int(dy)).RGBA() dstColorRGBA64.R = uint16(qr*pa1/0xffff + pr) dstColorRGBA64.G = uint16(qg*pa1/0xffff + pg) dstColorRGBA64.B = uint16(qb*pa1/0xffff + pb) dstColorRGBA64.A = uint16(qa*pa1/0xffff + pa) dst.Set(dr.Min.X+int(dx), dr.Min.Y+int(dy), dstColor) } } } case Src: switch dst := dst.(type) { case *image.RGBA: pr, pg, pb, pa := src.C.RGBA() pr8 := uint8(pr >> 8) pg8 := uint8(pg >> 8) pb8 := uint8(pb >> 8) pa8 := uint8(pa >> 8) for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 d := dst.PixOffset(dr.Min.X+adr.Min.X, dr.Min.Y+int(dy)) for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx, d = dx+1, d+4 { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } dst.Pix[d+0] = pr8 dst.Pix[d+1] = pg8 dst.Pix[d+2] = pb8 dst.Pix[d+3] = pa8 } } default: pr, pg, pb, pa := src.C.RGBA() dstColorRGBA64 := &color.RGBA64{ uint16(pr), uint16(pg), uint16(pb), uint16(pa), } dstColor := color.Color(dstColorRGBA64) for dy := int32(adr.Min.Y); dy < int32(adr.Max.Y); dy++ { dyf := float64(dr.Min.Y+int(dy)) + 0.5 for dx := int32(adr.Min.X); dx < int32(adr.Max.X); dx++ { dxf := float64(dr.Min.X+int(dx)) + 0.5 sx0 := int(d2s[0]*dxf+d2s[1]*dyf+d2s[2]) + bias.X sy0 := int(d2s[3]*dxf+d2s[4]*dyf+d2s[5]) + bias.Y if !(image.Point{sx0, sy0}).In(sr) { continue } dst.Set(dr.Min.X+int(dx), dr.Min.Y+int(dy), dstColor) } } } } }