Updates to match new primary repo.
This commit is contained in:
56
gradient.go
56
gradient.go
@@ -3,6 +3,13 @@ package main
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
GRAD_IDX_TOP = 0
|
||||
GRAD_IDX_BOTTOM = 1
|
||||
)
|
||||
|
||||
func HexToRGBA(hexcolor int) color.RGBA {
|
||||
@@ -14,13 +21,36 @@ func HexToRGBA(hexcolor int) color.RGBA {
|
||||
}
|
||||
}
|
||||
|
||||
// performs linear gradient fill on target image for the background from top to bottom colors
|
||||
func FillGradient(target *image.RGBA, width int, height int, top, bottom color.RGBA) {
|
||||
type Gradient struct {
|
||||
Scene *ebiten.Image //our resultant image
|
||||
target *image.RGBA //raw pixel array
|
||||
width int
|
||||
height int
|
||||
colors []color.RGBA //gradient boundary colors
|
||||
}
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
func NewGradient(width, height int) *Gradient {
|
||||
return &Gradient{
|
||||
Scene: ebiten.NewImage(width, height),
|
||||
target: image.NewRGBA(image.Rect(0, 0, width, height)),
|
||||
width: width,
|
||||
height: height,
|
||||
colors: []color.RGBA{HexToRGBA(0xFFFFFF), HexToRGBA(0x000000)},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gradient) SetColors(top, bottom color.RGBA) {
|
||||
g.colors[GRAD_IDX_TOP] = top
|
||||
g.colors[GRAD_IDX_BOTTOM] = bottom
|
||||
g.fillCurrent()
|
||||
}
|
||||
|
||||
// performs linear fill on the currently set gradient values, top to bottom (0 to 1)
|
||||
func (g *Gradient) fill(top, bottom color.RGBA) {
|
||||
for y := 0; y < g.height; y++ {
|
||||
|
||||
//the percent of our transition informs our blending
|
||||
percentIn := float64(y) / float64(height)
|
||||
percentIn := float64(y) / float64(g.height)
|
||||
|
||||
//compute top and bottom blend values as factor of percentage
|
||||
pTopR := float64(top.R) * (1 - percentIn)
|
||||
@@ -32,12 +62,18 @@ func FillGradient(target *image.RGBA, width int, height int, top, bottom color.R
|
||||
pBottomB := float64(bottom.B) * (percentIn)
|
||||
|
||||
//perform blending on per pixel row basis
|
||||
for x := 0; x < width; x++ {
|
||||
pixelIndex := 4 * (width*y + x)
|
||||
target.Pix[pixelIndex] = uint8(pTopR + pBottomR)
|
||||
target.Pix[pixelIndex+1] = uint8(pTopG + pBottomG)
|
||||
target.Pix[pixelIndex+2] = uint8(pTopB + pBottomB)
|
||||
target.Pix[pixelIndex+3] = 0xff
|
||||
for x := 0; x < g.width; x++ {
|
||||
pixelIndex := 4 * (g.width*y + x)
|
||||
g.target.Pix[pixelIndex] = uint8(pTopR + pBottomR)
|
||||
g.target.Pix[pixelIndex+1] = uint8(pTopG + pBottomG)
|
||||
g.target.Pix[pixelIndex+2] = uint8(pTopB + pBottomB)
|
||||
g.target.Pix[pixelIndex+3] = 0xff
|
||||
}
|
||||
}
|
||||
g.Scene.WritePixels(g.target.Pix)
|
||||
}
|
||||
|
||||
// gradient on currently set colors
|
||||
func (g *Gradient) fillCurrent() {
|
||||
g.fill(g.colors[GRAD_IDX_TOP], g.colors[GRAD_IDX_BOTTOM])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user