Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Aug 1, 2024
1 parent 7e28c1c commit 6a04687
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 111 deletions.
54 changes: 27 additions & 27 deletions basis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import (
// https://github.com/d3/d3-interpolate/blob/master/src/basis.js

type basisGradient struct {
colors [][4]float64
pos []float64
dmin float64
dmax float64
mode BlendMode
firstColor Color
lastColor Color
colors [][4]float64
positions []float64
min float64
max float64
mode BlendMode
first Color
last Color
}

func (lg basisGradient) At(t float64) Color {
if t <= lg.dmin {
return lg.firstColor
if t <= lg.min {
return lg.first
}

if t >= lg.dmax {
return lg.lastColor
if t >= lg.max {
return lg.last
}

if math.IsNaN(t) {
return Color{R: 0, G: 0, B: 0, A: 1}
return Color{A: 1}
}

low := 0
high := len(lg.pos)
high := len(lg.positions)
n := high - 1

for low < high {
mid := (low + high) / 2
if lg.pos[mid] < t {
if lg.positions[mid] < t {
low = mid + 1
} else {
high = mid
Expand All @@ -46,8 +46,8 @@ func (lg basisGradient) At(t float64) Color {
low = 1
}

p1 := lg.pos[low-1]
p2 := lg.pos[low]
p1 := lg.positions[low-1]
p2 := lg.positions[low]
val0 := lg.colors[low-1]
val1 := lg.colors[low]
i := low - 1
Expand Down Expand Up @@ -81,24 +81,24 @@ func (lg basisGradient) At(t float64) Color {
return Oklab(a, b, c, d).Clamp()
}

return Color{R: 0, G: 0, B: 0, A: 0}
return Color{}
}

func newBasisGradient(colors []Color, pos []float64, mode BlendMode) Gradient {
func newBasisGradient(colors []Color, positions []float64, mode BlendMode) Gradient {
gradbase := basisGradient{
colors: convertColors(colors, mode),
pos: pos,
dmin: pos[0],
dmax: pos[len(pos)-1],
mode: mode,
firstColor: colors[0],
lastColor: colors[len(colors)-1],
colors: convertColors(colors, mode),
positions: positions,
min: positions[0],
max: positions[len(positions)-1],
mode: mode,
first: colors[0],
last: colors[len(colors)-1],
}

return Gradient{
grad: gradbase,
dmin: pos[0],
dmax: pos[len(pos)-1],
dmin: positions[0],
dmax: positions[len(positions)-1],
}
}

Expand Down
50 changes: 25 additions & 25 deletions catmull_rom.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ func toCatmullRomSegments(values []float64) [][4]float64 {
}

type catmullRomGradient struct {
segments [][4][4]float64
positions []float64
dmin float64
dmax float64
mode BlendMode
firstColor Color
lastColor Color
segments [][4][4]float64
positions []float64
min float64
max float64
mode BlendMode
first Color
last Color
}

func newCatmullRomGradient(colors []Color, pos []float64, space BlendMode) Gradient {
func newCatmullRomGradient(colors []Color, positions []float64, space BlendMode) Gradient {
n := len(colors)
a := make([]float64, n)
b := make([]float64, n)
Expand Down Expand Up @@ -90,35 +90,35 @@ func newCatmullRomGradient(colors []Color, pos []float64, space BlendMode) Gradi
s4[i],
}
}
dmin := pos[0]
dmax := pos[n-1]
min := positions[0]
max := positions[n-1]
gradbase := catmullRomGradient{
segments: segments,
positions: pos,
dmin: dmin,
dmax: dmax,
mode: space,
firstColor: colors[0],
lastColor: colors[len(colors)-1],
segments: segments,
positions: positions,
min: min,
max: max,
mode: space,
first: colors[0],
last: colors[len(colors)-1],
}
return Gradient{
grad: gradbase,
dmin: dmin,
dmax: dmax,
dmin: min,
dmax: max,
}
}

func (g catmullRomGradient) At(t float64) Color {
if math.IsNaN(t) {
return Color{R: 0, G: 0, B: 0, A: 1}
return Color{A: 1}
}

if t <= g.dmin {
return g.firstColor
if t <= g.min {
return g.first
}

if t >= g.dmax {
return g.lastColor
if t >= g.max {
return g.last
}

low := 0
Expand Down Expand Up @@ -162,5 +162,5 @@ func (g catmullRomGradient) At(t float64) Color {
return Oklab(a, b, c, d).Clamp()
}

return Color{R: 0, G: 0, B: 0, A: 0}
return Color{}
}
14 changes: 7 additions & 7 deletions gimp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ type gimpSegment struct {

type gimpGradient struct {
segments []gimpSegment
dmin float64
dmax float64
min float64
max float64
}

func (ggr gimpGradient) At(t float64) Color {
if t <= ggr.dmin {
if t <= ggr.min {
return ggr.segments[0].lcolor
}

if t >= ggr.dmax {
if t >= ggr.max {
return ggr.segments[len(ggr.segments)-1].rcolor
}

if math.IsNaN(t) {
return Color{R: 0, G: 0, B: 0, A: 1}
return Color{A: 1}
}

low := 0
Expand Down Expand Up @@ -277,8 +277,8 @@ func ParseGgr(r io.Reader, fg, bg Color) (Gradient, string, error) {

gradbase := gimpGradient{
segments: segments,
dmin: 0,
dmax: 1,
min: 0,
max: 1,
}

return Gradient{
Expand Down
7 changes: 6 additions & 1 deletion gimp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func Test_GIMPGradient(t *testing.T) {
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 0 0 0 1 1 1 1 1 0 0 1 3"
grad, name, err = ParseGgr(strings.NewReader(ggr), red, blue)
test(t, err, nil)
test(t, name, "My Gradient")
test(t, grad.At(0).HexString(), "#ff0000")
test(t, grad.At(1).HexString(), "#0000ff")

// Blending function: step
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 0 0 1 0 0 1 1 5 0 0 0"
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
test(t, err, nil)
test(t, name, "My Gradient")
test(t, grad.At(0.00).HexString(), "#ff0000")
test(t, grad.At(0.25).HexString(), "#ff0000")
test(t, grad.At(0.49).HexString(), "#ff0000")
Expand All @@ -44,6 +46,7 @@ func Test_GIMPGradient(t *testing.T) {
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 1 1 1 0 0 1 1 0 1 0 0"
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
test(t, err, nil)
test(t, name, "My Gradient")
test(t, grad.At(0.0).HexString(), "#ffffff")
test(t, grad.At(0.5).HexString(), "#80ff80")
test(t, grad.At(1.0).HexString(), "#0000ff")
Expand All @@ -52,6 +55,7 @@ func Test_GIMPGradient(t *testing.T) {
ggr = "GIMP Gradient\nName: My Gradient\n1\n0 0.5 1 1 1 1 1 0 0 1 1 0 2 0 0"
grad, name, err = ParseGgr(strings.NewReader(ggr), black, black)
test(t, err, nil)
test(t, name, "My Gradient")
test(t, grad.At(0.0).HexString(), "#ffffff")
test(t, grad.At(0.5).HexString(), "#ff80ff")
test(t, grad.At(1.0).HexString(), "#0000ff")
Expand All @@ -60,7 +64,8 @@ func Test_GIMPGradient(t *testing.T) {

data := []string{
"",
"GIMP Pallete",
" ",
"GIMP Palette\nName: Gold\n#\n252 252 128",
"GIMP Gradient\nxx",
"GIMP Gradient\nName: Gradient\nx",
"GIMP Gradient\nName: Gradient\n1\n0 0 0",
Expand Down
54 changes: 27 additions & 27 deletions linear.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
)

type linearGradient struct {
colors [][4]float64
pos []float64
dmin float64
dmax float64
mode BlendMode
firstColor Color
lastColor Color
colors [][4]float64
positions []float64
min float64
max float64
mode BlendMode
first Color
last Color
}

func (lg linearGradient) At(t float64) Color {
if t <= lg.dmin {
return lg.firstColor
if t <= lg.min {
return lg.first
}

if t >= lg.dmax {
return lg.lastColor
if t >= lg.max {
return lg.last
}

if math.IsNaN(t) {
return Color{R: 0, G: 0, B: 0, A: 1}
return Color{A: 1}
}

low := 0
high := len(lg.pos)
high := len(lg.positions)

for low < high {
mid := (low + high) / 2
if lg.pos[mid] < t {
if lg.positions[mid] < t {
low = mid + 1
} else {
high = mid
Expand All @@ -43,8 +43,8 @@ func (lg linearGradient) At(t float64) Color {
low = 1
}

p1 := lg.pos[low-1]
p2 := lg.pos[low]
p1 := lg.positions[low-1]
p2 := lg.positions[low]
t = (t - p1) / (p2 - p1)
a, b, c, d := linearInterpolate(lg.colors[low-1], lg.colors[low], t)

Expand All @@ -57,23 +57,23 @@ func (lg linearGradient) At(t float64) Color {
return Oklab(a, b, c, d).Clamp()
}

return Color{R: 0, G: 0, B: 0, A: 0}
return Color{}
}

func newLinearGradient(colors []Color, pos []float64, mode BlendMode) Gradient {
func newLinearGradient(colors []Color, positions []float64, mode BlendMode) Gradient {
gradbase := linearGradient{
colors: convertColors(colors, mode),
pos: pos,
dmin: pos[0],
dmax: pos[len(pos)-1],
mode: mode,
firstColor: colors[0],
lastColor: colors[len(colors)-1],
colors: convertColors(colors, mode),
positions: positions,
min: positions[0],
max: positions[len(positions)-1],
mode: mode,
first: colors[0],
last: colors[len(colors)-1],
}

return Gradient{
grad: gradbase,
dmin: pos[0],
dmax: pos[len(pos)-1],
dmin: positions[0],
dmax: positions[len(positions)-1],
}
}
Loading

0 comments on commit 6a04687

Please sign in to comment.