// BlendBgColor blends the Image (receiver) into the background color
// specified. This is more efficient than creating a background image and
// blending with Blend.
500870
func BlendBgColor(dest *Image, c color.Color) {
	r := dest.Bounds()
	cr32, cg32, cb32, _ := c.RGBA()
	cr, cg, cb := uint8(cr32), uint8(cg32), uint8(cb32)

	var bgra BGRA
	var alpha float64
	for x := r.Min.X; x < r.Max.X; x++ {
		for y := r.Min.Y; y < r.Max.Y; y++ {
			bgra = dest.At(x, y).(BGRA)
			alpha = float64(bgra.A) / 255.0
			dest.SetBGRA(x, y, BGRA{
				B: blend(cb, bgra.B, alpha),
				G: blend(cg, bgra.G, alpha),
				R: blend(cr, bgra.R, alpha),
				A: 0xff,
			})
		}
	}
}
995364
func (c1 Color) BlendLuv(c2 Color, t float64) Color {
	l1, u1, v1 := c1.Luv()
	l2, u2, v2 := c2.Luv()
	return Luv(l1+t*(l2-l1),
		u1+t*(u2-u1),
		v1+t*(v2-v1))
}
2273
func Gamma(src image.Image, gamma float64) *image.RGBA {
	gamma = math.Max(0.00001, gamma)

	lookup := make([]uint8, 256)

	for i := 0; i < 256; i++ {
		lookup[i] = uint8(f64.Clamp(math.Pow(float64(i)/255, 1.0/gamma)*255, 0, 255))
	}

	fn := func(c color.RGBA) color.RGBA {
		return color.RGBA{lookup[c.R], lookup[c.G], lookup[c.B], c.A}
	}

	img := Apply(src, fn)

	return img
}
224933
func BgBrightCyan(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.BgBrightCyan()
	}
	return value{value: arg, color: BrightBg | CyanBg}
}
367199
func OptionSelectedSuggestionBGColor(x Color) Option {
	return func(p *Prompt) error {
		p.renderer.selectedSuggestionBGColor = x
		return nil
	}
}
496049
func Background(str string, color int) string {
	return applyTransform(str, func(idx int, line string) string {
		return fmt.Sprintf("%s%s%s", getBgColor(color), line, RESET)
	})
}
67930
func BgGreen(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.BgGreen()
	}
	return value{value: arg, color: GreenBg}
}
143573
func (w *Waveform) setFGColorFunction(function ColorFunc) error {
	// Function cannot be nil
	if function == nil {
		return errFGColorFunctionNil
	}

	w.fgColorFn = function

	return nil
}
1326415
func blend(d, s uint8, alpha float64) uint8 {
	return uint8(float64(s)*alpha + float64(d)*(1-alpha))
}
252109
func BgBlack(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.BgBlack()
	}
	return value{value: arg, color: BlackBg}
}
1226411
func (b *BlinkMDriver) Color() (color []byte, err error) {
	if _, err = b.connection.Write([]byte("g")); err != nil {
		return
	}
	data := []byte{0, 0, 0}
	read, err := b.connection.Read(data)
	if read != 3 || err != nil {
		return []byte{}, err
	}
	return []byte{data[0], data[1], data[2]}, nil
}
433223
func BgBrightBlack(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.BgBrightBlack()
	}
	return value{value: arg, color: BrightBg | BlackBg}
}
901640
func NewBackground(world *ecs.World, width, height int, colorA, colorB color.Color) *Background {
	rect := image.Rect(0, 0, width, height)

	img := image.NewNRGBA(rect)
	for i := rect.Min.X; i < rect.Max.X; i++ {
		for j := rect.Min.Y; j < rect.Max.Y; j++ {
			if i%40 > 20 {
				if j%40 > 20 {
					img.Set(i, j, colorA)
				} else {
					img.Set(i, j, colorB)
				}
			} else {
				if j%40 > 20 {
					img.Set(i, j, colorB)
				} else {
					img.Set(i, j, colorA)
				}
			}
		}
	}

	bgTexture := common.NewImageObject(img)

	bg := &Background{BasicEntity: ecs.NewBasic()}
	bg.RenderComponent = common.RenderComponent{Drawable: common.NewTextureSingle(bgTexture)}
	bg.SpaceComponent = common.SpaceComponent{
		Position: engo.Point{0, 0},
		Width:    float32(width),
		Height:   float32(height),
	}

	for _, system := range world.Systems() {
		switch sys := system.(type) {
		case *common.RenderSystem:
			sys.Add(&bg.BasicEntity, &bg.RenderComponent, &bg.SpaceComponent)
		}
	}

	return bg
}
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
}
149235
func (w *Waveform) SetFGColorFunction(function ColorFunc) error {
	return w.SetOptions(FGColorFunction(function))
}
28370
func (c BGRA) RGBA() (r, g, b, a uint32) {
	r = uint32(c.R)
	r |= r << 8

	g = uint32(c.G)
	g |= g << 8

	b = uint32(c.B)
	b |= b << 8

	a = uint32(c.A)
	a |= a << 8

	return
}
718127
func EllipseColor(renderer *sdl.Renderer, x, y, rx, ry int32, color sdl.Color) bool {
	_x := C.Sint16(x)
	_y := C.Sint16(y)
	_rx := C.Sint16(rx)
	_ry := C.Sint16(ry)
	_color := C.Uint32(gfxColor(color))
	return C.ellipseColor((*C.SDL_Renderer)(unsafe.Pointer(renderer)), _x, _y, _rx, _ry, _color) == 0
}