Files
pobounty/gradient.go

44 lines
1.2 KiB
Go
Raw Normal View History

2023-09-17 13:34:03 -04:00
package main
import (
"image"
"image/color"
)
func HexToRGBA(hexcolor int) color.RGBA {
return color.RGBA{
R: uint8(hexcolor >> 0x10 & 0xff),
G: uint8(hexcolor >> 0x08 & 0xff),
B: uint8(hexcolor >> 0x00 & 0xff),
A: 0xff,
}
}
// 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) {
for y := 0; y < height; y++ {
//the percent of our transition informs our blending
percentIn := float64(y) / float64(height)
//compute top and bottom blend values as factor of percentage
pTopR := float64(top.R) * (1 - percentIn)
pTopG := float64(top.G) * (1 - percentIn)
pTopB := float64(top.B) * (1 - percentIn)
pBottomR := float64(bottom.R) * (percentIn)
pBottomG := float64(bottom.G) * (percentIn)
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
}
}
}