Skip to content

Commit

Permalink
add GoColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Aug 2, 2024
1 parent 2b2df15 commit 1c00e98
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
35 changes: 28 additions & 7 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package colorgrad

import (
"image/color"
"testing"
)

Expand Down Expand Up @@ -61,24 +62,44 @@ func Test_Builder(t *testing.T) {
test(t, grad.At(90).HexString(), "#0000ff")

// Multiple colors, custom domain
grad, err = NewGradient().
HtmlColors("#00f", "#00ffff").
Colors(Rgb8(255, 255, 0, 255), Hwb(0, 0, 0, 1)).
HtmlColors("gold").
gb := NewGradient()
grad, err = gb.HtmlColors("#00f", "#00ffff").
Colors(
Rgb8(255, 255, 0, 255),
Hwb(320, 0.1, 0.3, 1),
GoColor(color.RGBA{R: 127, G: 0, B: 0, A: 127}),
GoColor(color.Gray{185}),
).
HtmlColors("gold", "hwb(320, 10%, 30%)").
Domain(10, 50).
Mode(BlendRgb).
Interpolation(InterpolationLinear).
Build()
test(t, err, nil)
test(t, domain(grad.Domain()), [2]float64{10, 50})
testSlice(t, colors2hex(grad.Colors(5)), []string{
testSlice(t, colors2hex(grad.Colors(8)), []string{
"#0000ff",
"#00ffff",
"#ffff00",
"#ff0000",
"#b31980", // xxx
"#ff00007f",
"#b9b9b9",
"#ffd700",
"#b31a80",
})
testSlice(t, colors2hex(*gb.GetColors()), []string{
"#0000ff",
"#00ffff",
"#ffff00",
"#b31a80",
"#ff00007f",
"#b9b9b9",
"#ffd700",
"#b31a80",
})

// Filter stops
gb := NewGradient()
gb = NewGradient()
gb.HtmlColors("gold", "red", "blue", "yellow", "black", "white", "plum")
gb.Domain(0, 0, 0.5, 0.5, 0.5, 1, 1)
_, err = gb.Build()
Expand Down
15 changes: 15 additions & 0 deletions gradient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package colorgrad

import (
"image/color"
"math"

"github.com/mazznoer/csscolorparser"
Expand Down Expand Up @@ -63,6 +64,20 @@ func Rgb8(r, g, b, a uint8) Color {
return Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255, A: float64(a) / 255}
}

func GoColor(col color.Color) Color {
r, g, b, a := col.RGBA()
if a == 0 {
return csscolorparser.Color{}
}
r *= 0xffff
r /= a
g *= 0xffff
g /= a
b *= 0xffff
b /= a
return csscolorparser.Color{R: float64(r) / 65535.0, G: float64(g) / 65535.0, B: float64(b) / 65535.0, A: float64(a) / 65535.0}
}

type gradientBase interface {
// Get color at certain position
At(float64) Color
Expand Down
13 changes: 13 additions & 0 deletions gradient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package colorgrad

import (
"fmt"
"image/color"
"testing"
)

Expand All @@ -10,6 +11,18 @@ func Test_Basic(t *testing.T) {
test(t, Rgb8(46, 139, 87, 255).HexString(), "#2e8b57")
test(t, Hwb(330, 0.4118, 0, 1).HexString(), "#ff69b4")

// Go color
test(t, GoColor(color.RGBA{R: 255, G: 0, B: 0, A: 255}).HexString(), "#ff0000")
test(t, GoColor(color.RGBA{R: 127, G: 0, B: 0, A: 127}).HexString(), "#ff00007f")
test(t, GoColor(color.RGBA{R: 0, G: 0, B: 0, A: 0}).HexString(), "#00000000")

test(t, GoColor(color.NRGBA{R: 0, G: 255, B: 0, A: 255}).HexString(), "#00ff00")
test(t, GoColor(color.NRGBA{R: 0, G: 255, B: 0, A: 127}).HexString(), "#00ff007f")

test(t, GoColor(color.Gray{0}).HexString(), "#000000")
test(t, GoColor(color.Gray{127}).HexString(), "#7f7f7f")

// Enums
test(t, BlendRgb.String(), "BlendRgb")
test(t, fmt.Sprintf("%s", BlendLinearRgb), "BlendLinearRgb")
test(t, fmt.Sprintf("%v", BlendOklab), "BlendOklab")
Expand Down

0 comments on commit 1c00e98

Please sign in to comment.