// Palette performs color quantization and returns a quant.Palette object.
//
// Returned is a palette with no more than q colors. Q may be > 256.
325732
func (q Quantizer) Palette(img image.Image) quant.Palette {
	qz := newQuantizer(img, int(q))
	if q > 1 {
		qz.cluster() // cluster pixels by color
	}
	return qz.t
}
723529
func Magenta(ctx context.Context, format string, args ...interface{}) {
	if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
		return
	}
	fmt.Fprint(Stdout, color.MagentaString(Prefix(ctx)+format+newline(ctx), args...))
}
555471
func getColor(color string) string {
	var colorcode string

	if value, ok := colors[color]; ok {
		colorcode = value
	} else {
		colorcode = colors["default"]
	}

	return colorcode
}
804392
func (c *Canvas) Color(at pixel.Vec) pixel.RGBA {
	return c.gf.Color(at)
}
295826
func (i *Image) Colourspace(c Interpretation) ([]byte, error) {
	options := Options{Interpretation: c}
	return i.Process(options)
}
585306
func Color(str string, color int) string {
	return applyTransform(str, func(idx int, line string) string {
		return fmt.Sprintf("%s%s%s", getColor(color), line, RESET)
	})
}
515741
func (palette *Palette) Free() {
	C.SDL_FreePalette((*C.SDL_Palette)(unsafe.Pointer(palette)))
}
60572
func ToRGBA(c color.Color) RGBA {
	if c, ok := c.(RGBA); ok {
		return c
	}
	r, g, b, a := c.RGBA()
	return RGBA{
		float64(r) / 0xffff,
		float64(g) / 0xffff,
		float64(b) / 0xffff,
		float64(a) / 0xffff,
	}
}
478247
func (palette *Palette) SetColors(colors []Color) error {
	if colors == nil {
		return nil
	}
	var ptr *C.SDL_Color
	if len(colors) > 0 {
		ptr = (*C.SDL_Color)(unsafe.Pointer(&colors[0]))
	}

	r := C.SDL_SetPaletteColors((*C.SDL_Palette)(unsafe.Pointer(palette)),
		ptr, 0, C.int(len(colors)))
	if r != 0 {
		return GetError()
	}
	return nil
}
1303153
func Black(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.Black()
	}
	return value{value: arg, color: BlackFg}
}
293336
func (s Style) Foreground(c Color) Style {
	if c == ColorDefault {
		return (s &^ (0x1ffffff00000000 | styleFgSet))
	}
	return (s &^ Style(0x1ffffff00000000)) |
		((Style(c) & 0x1ffffff) << 32) | styleFgSet
}
1200365
func GetColor(bitmap C.MMBitmapRef, x, y int) C.MMRGBHex {
	color := C.bitmap_get_color(bitmap, C.size_t(x), C.size_t(y))

	return color
}
1340822
func (c Color) Hex() int32 {
	if c&ColorIsRGB != 0 {
		return (int32(c) & 0xffffff)
	}
	if v, ok := ColorValues[c]; ok {
		return v
	}
	return -1
}
166018
func (col Color) Hex() string {
	// Add 0.5 for rounding
	return fmt.Sprintf("#%02x%02x%02x", uint8(col.R*255.0+0.5), uint8(col.G*255.0+0.5), uint8(col.B*255.0+0.5))
}
1077830
func b1(img *image.Paletted, x, y, size float64, angle int) {
	isize := int(size)
	ix := int(x)
	iy := int(y)
	for i := ix + 1; i < ix+isize; i++ {
		for j := iy + 1; j < iy+isize; j++ {
			img.SetColorIndex(i, j, 1)
		}
	}
}
516181
func (c Color) RGB() (int32, int32, int32) {
	v := c.Hex()
	if v < 0 {
		return -1, -1, -1
	}
	return (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff
}
594871
func dither211(i0 image.Image, cp color.Palette) *image.Paletted {
	if len(cp) > 256 {
		// representation limit of image.Paletted.  a little sketchy to return
		// nil, but unworkable results are always better than wrong results.
		return nil
	}
	b := i0.Bounds()
	pi := image.NewPaletted(b, cp)
	if b.Empty() {
		return pi // no work to do
	}
	sp := make(sPalette, len(cp))
	for i, c := range cp {
		r, g, b, _ := c.RGBA()
		sp[i] = sRGB{int32(r), int32(g), int32(b)}
	}
	// afc is adjustd full color.  e, rt, dn hold diffused errors.
	var afc, e, rt sRGB
	dn := make([]sRGB, b.Dx()+1)
	for y := b.Min.Y; y < b.Max.Y; y++ {
		rt = dn[0]
		dn[0] = sRGB{}
		for x := b.Min.X; x < b.Max.X; x++ {
			// full color from original image
			r0, g0, b0, _ := i0.At(x, y).RGBA()
			// adjusted full color = original color + diffused error
			afc.r = int32(r0) + rt.r>>2
			afc.g = int32(g0) + rt.g>>2
			afc.b = int32(b0) + rt.b>>2
			// clipping or clamping is usually explained as necessary
			// to avoid integer overflow but with palettes that do not
			// represent the full color space of the image, it is needed
			// to keep areas of excess color from saturating at palette
			// limits and bleeding into neighboring areas.
			if afc.r < 0 {
				afc.r = 0
			} else if afc.r > 0xffff {
				afc.r = 0xffff
			}
			if afc.g < 0 {
				afc.g = 0
			} else if afc.g > 0xffff {
				afc.g = 0xffff
			}
			if afc.b < 0 {
				afc.b = 0
			} else if afc.b > 0xffff {
				afc.b = 0xffff
			}
			// nearest palette entry
			i := sp.index(afc)
			// set pixel in destination image
			pi.SetColorIndex(x, y, uint8(i))
			// error to be diffused = full color - palette color.
			pc := sp[i]
			e.r = afc.r - pc.r
			e.g = afc.g - pc.g
			e.b = afc.b - pc.b
			// half of error*4 goes right
			dx := x - b.Min.X + 1
			rt.r = dn[dx].r + e.r*2
			rt.g = dn[dx].g + e.g*2
			rt.b = dn[dx].b + e.b*2
			// the other half goes down
			dn[dx] = e
			dn[dx-1].r += e.r
			dn[dx-1].g += e.g
			dn[dx-1].b += e.b
		}
	}
	return pi
}