1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
package randomcolor
import (
"image/color"
"math/rand"
)
// Luminosity stores the level of luminosity for the generated color.
type Luminosity int
const (
// LIGHT is used to generate light colors
LIGHT Luminosity = iota
// DARK is used to generate dark colors
DARK
// BRIGHT is used to generator bright colors
BRIGHT
// RANDOM is used to generate colors of random luminosity
RANDOM
)
// New returns a random color in the specified hue and luminosity.
func New(hue Color, lum Luminosity) color.Color {
c := HSV{}
c.H = setHue(hue)
c.S = setSaturation(c, hue, lum)
c.V = setBrightness(c, lum)
if c.H == 0 {
c.H = 1
}
if c.H == 360 {
c.H = 359
}
// Rebase the h,s,v values
c.H = c.H / 360
c.S = c.S / 100
c.V = c.V / 100
return c
}
// Range represents a range between lower (Range[0]) and upper bounds (Range[1]).
type Range [2]int
// Color represents a color in a specified range
type Color struct {
HueRange Range
LowerBounds []Range
}
// SaturationRange returns the minimum and maximum saturation for the color.
func (c Color) SaturationRange() Range {
sMin := c.LowerBounds[0][0]
sMax := c.LowerBounds[len(c.LowerBounds)-1][0]
return Range{sMin, sMax}
}
// BrightnessRange returns the minimum and maximum brigthness for the color.
func (c Color) BrightnessRange() Range {
bMin := c.LowerBounds[len(c.LowerBounds)-1][1]
bMax := c.LowerBounds[0][1]
return Range{bMin, bMax}
}
func setHue(c Color) float64 {
hue := randWithin(c.HueRange[0], c.HueRange[1])
if hue < 0 {
hue = 360 + hue
}
return float64(hue)
}
func setSaturation(hsv HSV, hue Color, lum Luminosity) float64 {
if hue.HueRange == Monochrome.HueRange {
return 0
}
saturationRange := ColorInfo(int(hsv.H)).SaturationRange()
var sMin = saturationRange[0]
var sMax = saturationRange[1]
switch lum {
case BRIGHT:
sMin = 55
case DARK:
sMin = sMax - 10
case LIGHT:
sMax = 55
case RANDOM:
return float64(randWithin(0, 100))
}
return float64(randWithin(sMin, sMax))
}
func setBrightness(hsv HSV, lum Luminosity) float64 {
bMin := getMinimumBrightness(hsv)
bMax := 100
switch lum {
case DARK:
bMax = bMin + 20
case LIGHT:
bMin = (bMax + bMin) / 2
case BRIGHT:
//
default:
bMin = 0
bMax = 100
}
return float64(randWithin(bMin, bMax))
}
func getMinimumBrightness(hsv HSV) int {
var lowerBounds []Range
lowerBounds = ColorInfo(int(hsv.H)).LowerBounds
for i := 0; i < (len(lowerBounds) - 1); i++ {
s1 := float64(lowerBounds[i][0])
v1 := float64(lowerBounds[i][1])
s2 := float64(lowerBounds[i+1][0])
v2 := float64(lowerBounds[i+1][1])
if hsv.S >= s1 && hsv.S <= s2 {
m := (v2 - v1) / (s2 - s1)
b := v1 - m*s1
return int(m*hsv.S + b)
}
}
return 0
}
func randWithin(first, last int) int {
return first + rand.Intn(last+1-first)
}
|