Skip to content

Commit

Permalink
Add cubehelix (#10)
Browse files Browse the repository at this point in the history
* cubehelix

* Remove warm, cool, & rainbow

Now using cubehelix

* Add files via upload

* Update README.md
  • Loading branch information
mazznoer committed Jul 27, 2020
1 parent aa9feb3 commit 2cbb9ba
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ grad.Colors(15)
`grad := colorgrad.Rainbow()`
![img](img/gradient-rainbow.png)

`grad := colorgrad.CubehelixDefault()`
![img](img/gradient-cubehelix-default.png)

`grad := colorgrad.Sinebow()`
![img](img/gradient-sinebow.png)

Expand Down
94 changes: 94 additions & 0 deletions gradfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// Algorithms adapted from: https://github.com/d3/d3-scale-chromatic

const deg2rad = math.Pi / 180
const pi_1_3 = math.Pi / 3
const pi_2_3 = math.Pi * 2 / 3

Expand Down Expand Up @@ -95,6 +96,99 @@ func (self cividisGradient) Colors(count uint) []colorful.Color {
return colors
}

// Cubehelix

type cubehelix struct {
h, s, l float64
}

func (c cubehelix) toColorful() colorful.Color {
h := (c.h + 120) * deg2rad
l := c.l
a := c.s * l * (1 - l)
cosh := math.Cos(h)
sinh := math.Sin(h)
r := (l - a*math.Min(0.14861*cosh-1.78277*sinh, 1.0))
g := (l - a*math.Min(0.29227*cosh+0.90649*sinh, 1.0))
b := l + a*(1.97294*cosh)
return colorful.Color{
R: clamp01(r),
G: clamp01(g),
B: clamp01(b),
}
}

func (c cubehelix) interpolate(c2 cubehelix, t float64) cubehelix {
return cubehelix{
h: c.h + t*(c2.h-c.h),
s: c.s + t*(c2.s-c.s),
l: c.l + t*(c2.l-c.l),
}
}

type cubehelixGradient struct {
start, end cubehelix
}

func CubehelixDefault() Gradient {
return cubehelixGradient{
start: cubehelix{300, 0.5, 0.0},
end: cubehelix{-240, 0.5, 1.0},
}
}

func Warm() Gradient {
return cubehelixGradient{
start: cubehelix{-100, 0.75, 0.35},
end: cubehelix{80, 1.50, 0.8},
}
}

func Cool() Gradient {
return cubehelixGradient{
start: cubehelix{260, 0.75, 0.35},
end: cubehelix{80, 1.50, 0.8},
}
}

func (self cubehelixGradient) At(t float64) colorful.Color {
return self.start.interpolate(self.end, t).toColorful()
}

func (self cubehelixGradient) Colors(count uint) []colorful.Color {
l := float64(count - 1)
colors := make([]colorful.Color, count)
for i := range colors {
colors[i] = self.At(float64(i) / l)
}
return colors
}

type rainbow struct{}

func Rainbow() Gradient {
return rainbow{}
}

func (self rainbow) At(t float64) colorful.Color {
t = math.Max(0, math.Min(1, t))
ts := math.Abs(t - 0.5)
return cubehelix{
h: 360*t - 100,
s: 1.5 - 1.5*ts,
l: 0.8 - 0.9*ts,
}.toColorful()
}

func (self rainbow) Colors(count uint) []colorful.Color {
l := float64(count - 1)
colors := make([]colorful.Color, count)
for i := range colors {
colors[i] = self.At(float64(i) / l)
}
return colors
}

func clamp01(t float64) float64 {
return math.Max(0, math.Min(1, t))
}
24 changes: 0 additions & 24 deletions gradset.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
package colorgrad

func Warm() Gradient {
colors := []string{"#6e40aa", "#8a3eb2", "#a83cb3", "#c53dad", "#df40a1", "#f4468f", "#ff507a", "#ff5e63", "#ff704e", "#ff843d", "#f89b31", "#e6b32e", "#d2c934", "#bfde43", "#aff05b"}
grad, _ := NewGradient().
HexColors(colors...).
Build()
return grad
}

func Cool() Gradient {
colors := []string{"#6e40aa", "#654ec0", "#585fd2", "#4973dd", "#3988e1", "#2b9ede", "#1fb3d3", "#1ac7c2", "#1bd9ac", "#24e695", "#34f07e", "#4df56a", "#6bf75c", "#8cf457", "#aff05b"}
grad, _ := NewGradient().
HexColors(colors...).
Build()
return grad
}

func Rainbow() Gradient {
colors := []string{"#6e40aa", "#a83cb3", "#df40a1", "#ff507a", "#ff704e", "#f89b31", "#d2c934", "#aff05b", "#6bf75c", "#34f07e", "#1bd9ac", "#1fb3d3", "#3988e1", "#585fd2", "#6e40aa"}
grad, _ := NewGradient().
HexColors(colors...).
Build()
return grad
}

func Viridis() Gradient {
colors := []string{"#440154", "#482777", "#3f4a8a", "#31678e", "#26838f", "#1f9d8a", "#6cce5a", "#b6de2b", "#fee825"}
grad, _ := NewGradient().
Expand Down
Binary file added img/gradient-cubehelix-default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2cbb9ba

Please sign in to comment.