Skip to content

Commit

Permalink
change behaviour of GradientBuilder's .colors() and .html_colors()
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Dec 30, 2020
1 parent 88bea9c commit 49bb1be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,11 @@ All preset gradients are in the domain 0..1.
grad1, err := colorgrad.NewGradient().
HtmlColors("#18dbf4", "#f6ff56").
Build()
```
![img](doc/images/gradient-normal.png)

```go
grad2 := grad1.Sharp(7)
```
![img](doc/images/gradient-normal.png)

![img](doc/images/gradient-sharp.png)

```go
Expand Down
20 changes: 5 additions & 15 deletions grad.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,23 @@ func NewGradient() *GradientBuilder {
}

func (gb *GradientBuilder) Colors(colors ...color.Color) *GradientBuilder {
gb.colors = make([]colorful.Color, len(colors))
for i, v := range colors {
c, _ := colorful.MakeColor(v)
gb.colors[i] = c
for _, c := range colors {
col, _ := colorful.MakeColor(c)
gb.colors = append(gb.colors, col)
}
return gb
}

func (gb *GradientBuilder) HtmlColors(htmlColors ...string) *GradientBuilder {
colors := []colorful.Color{}
invalidColors := []string{}

for _, s := range htmlColors {
c, err := csscolorparser.Parse(s)
if err != nil {
invalidColors = append(invalidColors, s)
gb.invalidHtmlColors = append(gb.invalidHtmlColors, s)
continue
}
col, _ := colorful.MakeColor(c)
colors = append(colors, col)
gb.colors = append(gb.colors, col)
}

if len(invalidColors) > 0 {
gb.invalidHtmlColors = invalidColors
}

gb.colors = colors
return gb
}

Expand Down
12 changes: 12 additions & 0 deletions grad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ func TestBasic2(t *testing.T) {
testStr(t, colors[2].Hex(), "#0000ff")

testStr(t, grad.At(math.NaN()).Hex(), "#ff0000")

// Custom colors #2
grad, _ = NewGradient().
HtmlColors("#00f", "#00ffff").
Colors(color.RGBA{255, 255, 0, 255}).
HtmlColors("lime").
Build()
colors = grad.ColorfulColors(4)
testStr(t, colors[0].Hex(), "#0000ff")
testStr(t, colors[1].Hex(), "#00ffff")
testStr(t, colors[2].Hex(), "#ffff00")
testStr(t, colors[3].Hex(), "#00ff00")
}

func TestError(t *testing.T) {
Expand Down

0 comments on commit 49bb1be

Please sign in to comment.