Skip to content

Commit

Permalink
Moved lerps into graphics2d/color
Browse files Browse the repository at this point in the history
  • Loading branch information
jphsd committed Mar 28, 2023
1 parent f2c1b87 commit f8184b7
Showing 1 changed file with 9 additions and 56 deletions.
65 changes: 9 additions & 56 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func (c *ColorConv) Eval2(x, y float64) color.Color {
default:
fallthrough
case LerpRGBA:
return ColorRGBALerp(nt, c1, c2)
return g2dcol.ColorRGBALerp(nt, c1, c2)
case LerpHSL:
return ColorHSLLerp(nt, c1, c2)
return g2dcol.ColorHSLLerp(nt, c1, c2)
case LerpHSLs:
return ColorHSLLerpS(nt, c1, c2)
return g2dcol.ColorHSLLerpS(nt, c1, c2)
}
}

Expand Down Expand Up @@ -198,11 +198,11 @@ func (c *ColorBlend) Eval2(x, y float64) color.Color {
default:
fallthrough
case LerpRGBA:
return ColorRGBALerp((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
return g2dcol.ColorRGBALerp((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
case LerpHSL:
return ColorHSLLerp((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
return g2dcol.ColorHSLLerp((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
case LerpHSLs:
return ColorHSLLerpS((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
return g2dcol.ColorHSLLerpS((c.Src3.Eval2(x, y)+1)/2, c.Src1.Eval2(x, y), c.Src2.Eval2(x, y))
}
}

Expand Down Expand Up @@ -233,54 +233,7 @@ func (c *ColorSubstitute) Eval2(x, y float64) color.Color {

// ColorRGBABiLerp calculates the color value at u, v [0,1] given colors at [0,0], [1,0], [0,1], [1,1] in RGB space.
func ColorRGBABiLerp(u, v float64, colors []color.Color) color.RGBA {
c1 := ColorRGBALerp(v, colors[0], colors[2])
c2 := ColorRGBALerp(v, colors[1], colors[3])
return ColorRGBALerp(u, c1, c2)
}

// ColorRGBALerp calculates the color value at t [0,1] given a start and end color in RGB space.
func ColorRGBALerp(t float64, start, end color.Color) color.RGBA {
rs, gs, bs, as := start.RGBA() // uint32 [0,0xffff]
re, ge, be, ae := end.RGBA()
rt := uint32(math.Floor((1-t)*float64(rs) + t*float64(re) + 0.5))
gt := uint32(math.Floor((1-t)*float64(gs) + t*float64(ge) + 0.5))
bt := uint32(math.Floor((1-t)*float64(bs) + t*float64(be) + 0.5))
at := uint32(math.Floor((1-t)*float64(as) + t*float64(ae) + 0.5))
rt >>= 8 // uint32 [0,0xff]
gt >>= 8
bt >>= 8
at >>= 8
return color.RGBA{uint8(rt), uint8(gt), uint8(bt), uint8(at)}
}

// ColorHSLLerp calculates the color value at t [0,1] given a start and end color in HSL space.
func ColorHSLLerp(t float64, start, end color.Color) color.Color {
cs, ce := g2dcol.NewHSL(start), g2dcol.NewHSL(end)
ht := (1-t)*cs.H + t*ce.H // Will never cross 1:0
st := (1-t)*cs.S + t*ce.S
lt := (1-t)*cs.L + t*ce.L
at := (1-t)*cs.A + t*ce.A
return g2dcol.HSL{ht, st, lt, at}
}

// ColorHSLLerpS calculates the color value at t [0,1] given a start and end color in HSL space.
// Differs from ColorHSLLerp in that the shortest path for hue is taken.
func ColorHSLLerpS(t float64, start, end color.Color) color.Color {
cs, ce := g2dcol.NewHSL(start), g2dcol.NewHSL(end)
hd := ce.H - cs.H
ht := 0.0
// Handle hue being circular
if hd > 0.5 || hd < -0.5 {
h := ce.H - 1
ht = (1-t)*cs.H + t*h
if ht < 0 {
ht += 1
}
} else {
ht = (1-t)*cs.H + t*ce.H
}
st := (1-t)*cs.S + t*ce.S
lt := (1-t)*cs.L + t*ce.L
at := (1-t)*cs.A + t*ce.A
return g2dcol.HSL{ht, st, lt, at}
c1 := g2dcol.ColorRGBALerp(v, colors[0], colors[2])
c2 := g2dcol.ColorRGBALerp(v, colors[1], colors[3])
return g2dcol.ColorRGBALerp(u, c1, c2)
}

0 comments on commit f8184b7

Please sign in to comment.