// Normal combines the foreground and background images by placing the foreground over the
// background using alpha compositing. The resulting image is then returned.
169665
func Normal(bg image.Image, fg image.Image) *image.RGBA {
	dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
		return alphaComp(c0, c1)
	})

	return dst
}
742482
func (pattern *Pattern) AddColorStopRGBA(offset, red, green, blue, alpha float64) {
	C.cairo_pattern_add_color_stop_rgba(pattern.Ptr, C.double(offset), C.double(red), C.double(green), C.double(blue), C.double(alpha))
	if err := pattern.status(); err != nil {
		panic(err)
	}
}
902645
func alphaComp(bg, fg fcolor.RGBAF64) fcolor.RGBAF64 {
	fg.Clamp()
	fga := fg.A

	r := (fg.R * fga / 1) + ((1 - fga) * bg.R / 1)
	g := (fg.G * fga / 1) + ((1 - fga) * bg.G / 1)
	b := (fg.B * fga / 1) + ((1 - fga) * bg.B / 1)
	a := bg.A + fga

	return fcolor.RGBAF64{R: r, G: g, B: b, A: a}
}
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,
			})
		}
	}
}
615193
func (m *HoltWintersMovAvgModel) Alpha(alpha float64) *HoltWintersMovAvgModel {
	m.alpha = &alpha
	return m
}
1297075
func intToRGBA(c uint32) color.RGBA {
	r := uint8((c >> 24) & 0xff)
	g := uint8((c >> 16) & 0xff)
	b := uint8((c >> 8) & 0xff)
	a := uint8(c & 0xff)
	return color.RGBA{r, g, b, a}
}
994122
func (p *Image) RGBAAt(x, y int) color.RGBA {
	if !(image.Point{x, y}.In(p.Rect)) {
		return color.RGBA{}
	}
	i := (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
	return color.RGBA{p.Pix[i+0], p.Pix[i+1], p.Pix[i+2], 0xFF}
}
841841
func renderBackground(svg *svg.SVG, x, y, w, h int) {
	svg.Rect(x, y, w, h, "fill:#eee") // TODO: Theme.
}
1114552
func Invert(src image.Image) *image.RGBA {
	fn := func(c color.RGBA) color.RGBA {
		return color.RGBA{255 - c.R, 255 - c.G, 255 - c.B, c.A}
	}

	img := adjust.Apply(src, fn)

	return img
}
414868
func NewStyle(background, foreground Paint) Style {
	bg := background
	fg := foreground
	return Style{
		bg,
		fg,
		computeColorCode(bg, fg),
	}
}
488239
func (m *HoltLinearMovAvgModel) Alpha(alpha float64) *HoltLinearMovAvgModel {
	m.alpha = &alpha
	return m
}
358249
func Add(bg image.Image, fg image.Image) *image.RGBA {
	dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
		r := c0.R + c1.R
		g := c0.G + c1.G
		b := c0.B + c1.B

		c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
		return alphaComp(c0, c2)
	})

	return dst
}
511643
func gradient(from, to color.RGBA, scale float64) (result color.RGBA) {
	scale = math.Max(math.Min(scale, 1.0), 0.0)
	result.R = linearScale(from.R, to.R, scale)
	result.G = linearScale(from.G, to.G, scale)
	result.B = linearScale(from.B, to.B, scale)
	return
}
3538
func (c colorBase) Regular(a ...interface{}) string {
	return color.New(color.Attribute(c)).Sprint(a...)
}
285367
func PatternCreateRGBA(red, green, blue, alpha float64) *Pattern {
	ret := wrapPattern(C.cairo_pattern_create_rgba(C.double(red), C.double(green), C.double(blue), C.double(alpha)))
	if err := ret.status(); err != nil {
		panic(err)
	}
	return ret
}
980277
func (matcher *EqualImageMatcher) Match(actual interface{}) (success bool, err error) {
	if actual == nil && matcher.Expected == nil {
		return false, fmt.Errorf("Refusing to compare <nil> to <nil>.\nBe explicit and use BeNil() instead.  This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.")
	}

	a, ok := actual.(image.Image)
	if !ok {
		return false, fmt.Errorf("actual must be of type image.Image")
	}

	e, ok := matcher.Expected.(image.Image)
	if !ok {
		return false, fmt.Errorf("expected must be of type image.Image")
	}

	if a.Bounds().Dx() != e.Bounds().Dx() {
		return false, fmt.Errorf("Different width values %d != %d", a.Bounds().Dx(), e.Bounds().Dx())
	}

	if a.Bounds().Dy() != e.Bounds().Dy() {
		return false, fmt.Errorf("Different height values %d != %d", a.Bounds().Dy(), e.Bounds().Dy())
	}

	for x := 0; x < a.Bounds().Dx(); x++ {
		for y := 0; y < a.Bounds().Dy(); y++ {
			if a.At(x, y) != e.At(x, y) {
				return false, fmt.Errorf("pixels don't match at %d %d. %d != %d", x, y, a.At(x, y), e.At(x, y))
			}
		}
	}

	return true, nil
}
445483
func Apply(img image.Image, fn func(color.RGBA) color.RGBA) *image.RGBA {
	bounds := img.Bounds()
	dst := clone.AsRGBA(img)
	w, h := bounds.Dx(), bounds.Dy()

	parallel.Line(h, func(start, end int) {
		for y := start; y < end; y++ {
			for x := 0; x < w; x++ {
				dstPos := y*dst.Stride + x*4

				c := color.RGBA{}

				c.R = dst.Pix[dstPos+0]
				c.G = dst.Pix[dstPos+1]
				c.B = dst.Pix[dstPos+2]
				c.A = dst.Pix[dstPos+3]

				c = fn(c)

				dst.Pix[dstPos+0] = c.R
				dst.Pix[dstPos+1] = c.G
				dst.Pix[dstPos+2] = c.B
				dst.Pix[dstPos+3] = c.A
			}
		}
	})

	return dst
}