| •1 |
|
| 2 |
package color |
| 3 |
|
| 4 |
import ( |
| 5 |
"image/color" |
| 6 |
|
| 7 |
"github.com/lucasb-eyer/go-colorful" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func Blend(size int, points ...color.Color) []color.Color { |
| 27 |
if size <= 0 || len(points) < 2 { |
| 28 |
return nil |
| 29 |
} |
| 30 |
if size == 1 { |
| 31 |
return []color.Color{points[0]} |
| 32 |
} |
| 33 |
|
| 34 |
stops := make([]colorful.Color, len(points)) |
| 35 |
for i, c := range points { |
| 36 |
stops[i], _ = colorful.MakeColor(c) |
| 37 |
} |
| 38 |
|
| 39 |
numSegments := len(stops) - 1 |
| 40 |
blended := make([]color.Color, 0, size) |
| 41 |
|
| 42 |
|
| 43 |
segmentSizes := make([]int, numSegments) |
| 44 |
baseSize := size / numSegments |
| 45 |
remainder := size % numSegments |
| 46 |
|
| 47 |
|
| 48 |
for i := range numSegments { |
| 49 |
segmentSizes[i] = baseSize |
| 50 |
if i < remainder { |
| 51 |
segmentSizes[i]++ |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
for i := range numSegments { |
| 57 |
c1 := stops[i] |
| 58 |
c2 := stops[i+1] |
| 59 |
segmentSize := segmentSizes[i] |
| 60 |
|
| 61 |
for j := range segmentSize { |
| 62 |
t := float64(j) / float64(segmentSize) |
| 63 |
c := c1.BlendHcl(c2, t) |
| 64 |
blended = append(blended, c) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return blended |
| 69 |
} |