rafactor colors as rgb objects

This commit is contained in:
Codeberg 2019-05-28 13:47:47 +02:00 committed by Holger Waechtler
parent c484b76c40
commit 526b5adead

113
main.go
View File

@ -77,6 +77,7 @@ type RGB struct {
r uint8
g uint8
b uint8
a uint8
}
type HSV struct {
@ -91,6 +92,7 @@ func f2rgb(r, g, b float64) *RGB {
c.r = uint8(r * 1.0 / 255)
c.g = uint8(g * 1.0 / 255)
c.b = uint8(b * 1.0 / 255)
c.a = 255
return c
}
@ -105,6 +107,7 @@ func f2hsv(h, s, v float64) *HSV {
func rgb(s string) *RGB {
c := new(RGB)
c.a = 255
fmt.Sscanf(s, "#%02x%02x%02x", &c.r, &c.g, &c.b)
return c
}
@ -166,40 +169,35 @@ func (c *HSV) to_rgb() *RGB {
return f2rgb(0, 0, 0)
}
func (c *RGB) brighterThan(ref RGB, delta uint8) string {
func (c *RGB) brighterThan(ref *RGB, delta float64) *RGB {
primary := c.to_hsv()
secondary := ref.to_hsv()
delta_f := float64(delta) * 1.0 / 255.0
if primary.v >= secondary.v+delta_f {
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
if primary.v >= secondary.v+delta {
return c
}
primary.v = secondary.v + delta_f
primary.v = secondary.v + delta
if primary.v > 360 {
primary.v = 360
}
c = primary.to_rgb()
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
return primary.to_rgb()
}
func (c *RGB) darkerThan(ref RGB, delta uint8) string {
func (c *RGB) darkerThan(ref *RGB, delta float64) *RGB {
primary := c.to_hsv()
secondary := ref.to_hsv()
delta_f := float64(delta) * 1.0 / 255.0
if primary.v <= secondary.v-delta_f {
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
if primary.v <= secondary.v-delta {
return c
}
primary.v = secondary.v - delta_f
primary.v = secondary.v - delta
if primary.v < 0 {
primary.v = 0
}
c = primary.to_rgb()
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
return primary.to_rgb()
}
func (c *RGB) brighterOrDarkerThan(ref RGB, delta uint8) string {
func (c *RGB) brighterOrDarkerThan(ref *RGB, delta float64) *RGB {
primary := c.to_hsv()
secondary := ref.to_hsv()
if primary.v <= secondary.v {
return c.darkerThan(ref, delta)
} else {
@ -207,22 +205,27 @@ func (c *RGB) brighterOrDarkerThan(ref RGB, delta uint8) string {
}
}
func (c RGB) withAlpha(alpha float64) string {
return fmt.Sprintf("#%02x%02x%02x%02x", c.r, c.g, c.b, uint8(255*alpha))
func (c *RGB) withAlpha(alpha float64) *RGB {
c.a = uint8(alpha * 255)
return c
}
func (c *RGB) html() string {
return fmt.Sprintf("#%02x%02x%02x%02x", c.r, c.g, c.b, c.a)
}
func maleAvatar(seed uint64) string {
var g = LinearCongruentialGenerator(seed)
var skinColor = g.pickOne([]string{"#FFDBAC", "#F5CFA0", "#EAC393", "#E0B687", "#CB9E6E", "#B68655", "#A26D3D", "#8D5524"})
var skinColor = rgb(g.pickOne([]string{"#FFDBAC", "#F5CFA0", "#EAC393", "#E0B687", "#CB9E6E", "#B68655", "#A26D3D", "#8D5524"}))
var hairColor = rgb(g.pickOne([]string{"#090806", "#2c222b", "#71635a", "#b7a69e", "#b89778", "#a56b46", "#b55239", "#8d4a43",
"#91553d", "#533d32", "#3b3024", "#554838", "#4e433f", "#504444", "#6a4e42", "#a7856a", "#977961"})).brighterOrDarkerThan(*rgb(skinColor), 17)
var eyesColor = g.pickOne([]string{"#76778b", "#697b94", "#647b90", "#5b7c8b", "#588387"})
var eyebrowsColor = rgb(rgb(hairColor).darkerThan(*rgb(skinColor), 7)).darkerThan(*rgb(hairColor), 10)
var mustacheColor = rgb(rgb(hairColor).darkerThan(*rgb(skinColor), 7)).withAlpha(g.pickOneFloat([]float64{1, 0.75, 0.5}))
var mouthColor = rgb(g.pickOne([]string{"#eec1ad", "#dbac98", "#d29985"})).brighterOrDarkerThan(*rgb(skinColor), 10)
var glassesColor = g.pickOne([]string{"#5f705c", "#43677d", "#5e172d", "#ffb67a", "#a04b5d", "#191919", "#323232", "#4b4b4b"})
var clothesColor = g.pickOne([]string{"#5bc0de", "#5cb85c", "#428bca", "#03396c", "#005b96", "#6497b1", "#1b85b8", "#5a5255", "#559e83", "#ae5a41", "#c3cb71", "#666547", "#ffe28a"})
var hatColor = g.pickOne([]string{"#18293b", "#2e1e05", "#989789", "#3d6ba7", "#517459", "#a62116"})
"#91553d", "#533d32", "#3b3024", "#554838", "#4e433f", "#504444", "#6a4e42", "#a7856a", "#977961"})).brighterOrDarkerThan(skinColor, 17)
var eyesColor = rgb(g.pickOne([]string{"#76778b", "#697b94", "#647b90", "#5b7c8b", "#588387"}))
var eyebrowsColor = hairColor.darkerThan(skinColor, 7).darkerThan(hairColor, 10)
var mustacheColor = hairColor.darkerThan(skinColor, 7).withAlpha(g.pickOneFloat([]float64{1, 0.75, 0.5}))
var mouthColor = rgb(g.pickOne([]string{"#eec1ad", "#dbac98", "#d29985"})).brighterOrDarkerThan(skinColor, 10)
var glassesColor = rgb(g.pickOne([]string{"#5f705c", "#43677d", "#5e172d", "#ffb67a", "#a04b5d", "#191919", "#323232", "#4b4b4b"}))
var clothesColor = rgb(g.pickOne([]string{"#5bc0de", "#5cb85c", "#428bca", "#03396c", "#005b96", "#6497b1", "#1b85b8", "#5a5255", "#559e83", "#ae5a41", "#c3cb71", "#666547", "#ffe28a"}))
var hatColor = rgb(g.pickOne([]string{"#18293b", "#2e1e05", "#989789", "#3d6ba7", "#517459", "#a62116"}))
var mood = ""
if mood == "" {
@ -358,15 +361,15 @@ func maleAvatar(seed uint64) string {
}, "")
m := []string{
"${skinColor}", skinColor,
"${hairColor}", hairColor,
"${eyesColor}", eyesColor,
"${eyebrowsColor}", eyebrowsColor,
"${mustacheColor}", mustacheColor,
"${mouthColor}", mouthColor,
"${glassesColor}", glassesColor,
"${clothesColor}", clothesColor,
"${hatColor}", hatColor,
"${skinColor}", skinColor.html(),
"${hairColor}", hairColor.html(),
"${eyesColor}", eyesColor.html(),
"${eyebrowsColor}", eyebrowsColor.html(),
"${mustacheColor}", mustacheColor.html(),
"${mouthColor}", mouthColor.html(),
"${glassesColor}", glassesColor.html(),
"${clothesColor}", clothesColor.html(),
"${hatColor}", hatColor.html(),
}
return strings.NewReplacer(m...).Replace(s)
}
@ -374,18 +377,18 @@ func maleAvatar(seed uint64) string {
func femaleAvatar(seed uint64) string {
var g = LinearCongruentialGenerator(seed)
var skinColor = g.pickOne([]string{"#FFDBAC", "#F5CFA0", "#EAC393", "#E0B687", "#CB9E6E", "#B68655", "#A26D3D", "#8D5524"})
var skinColor = rgb(g.pickOne([]string{"#FFDBAC", "#F5CFA0", "#EAC393", "#E0B687", "#CB9E6E", "#B68655", "#A26D3D", "#8D5524"}))
var hairColor = rgb(g.pickOne([]string{"#090806", "#2c222b", "#71635a", "#b7a69e", "#d6c4c2", "#cabfb1", "#dcd0ba", "#fff5e1",
"#e6cea8", "#e5c8a8", "#debc99", "#b89778", "#a56b46", "#b55239", "#8d4a43", "#91553d",
"#533d32", "#3b3024", "#554838", "#4e433f", "#504444", "#6a4e42", "#a7856a", "#977961"})).brighterOrDarkerThan(*rgb(skinColor), 17)
var eyesColor = g.pickOne([]string{"#76778b", "#697b94", "#647b90", "#5b7c8b", "#588387"})
var eyebrowsColor = rgb(rgb(hairColor).darkerThan(*rgb(skinColor), 7)).darkerThan(*rgb(hairColor), 10)
var accessoriesColor = g.pickOne([]string{"#daa520", "#ffd700", "#eee8aa", "#fafad2", "#d3d3d3", "#a9a9a9"})
var mouthColor = rgb(g.pickOne([]string{"#dbac98", "#d29985", "#c98276", "#e35d6a", "#e32153", "#de0f0d"})).brighterOrDarkerThan(*rgb(skinColor), 10)
var glassesColor = g.pickOne([]string{"#5f705c", "#43677d", "#5e172d", "#ffb67a", "#a04b5d", "#191919", "#323232", "#4b4b4b"})
var clothesColor = g.pickOne([]string{"#d11141", "#00b159", "#00aedb", "#f37735", "#ffc425", "#740001", "#ae0001", "#eeba30",
"#96ceb4", "#ffeead", "#ff6f69", "#ffcc5c", "#88d8b0"})
var hatColor = g.pickOne([]string{"#cc6192", "#2663a3", "#a62116", "#3d8a6b", "#614f8a"})
"#533d32", "#3b3024", "#554838", "#4e433f", "#504444", "#6a4e42", "#a7856a", "#977961"})).brighterOrDarkerThan(skinColor, 17)
var eyesColor = rgb(g.pickOne([]string{"#76778b", "#697b94", "#647b90", "#5b7c8b", "#588387"}))
var eyebrowsColor = hairColor.darkerThan(skinColor, 7).darkerThan(hairColor, 10)
var accessoriesColor = rgb(g.pickOne([]string{"#daa520", "#ffd700", "#eee8aa", "#fafad2", "#d3d3d3", "#a9a9a9"}))
var mouthColor = rgb(g.pickOne([]string{"#dbac98", "#d29985", "#c98276", "#e35d6a", "#e32153", "#de0f0d"})).brighterOrDarkerThan(skinColor, 10)
var glassesColor = rgb(g.pickOne([]string{"#5f705c", "#43677d", "#5e172d", "#ffb67a", "#a04b5d", "#191919", "#323232", "#4b4b4b"}))
var clothesColor = rgb(g.pickOne([]string{"#d11141", "#00b159", "#00aedb", "#f37735", "#ffc425", "#740001", "#ae0001", "#eeba30",
"#96ceb4", "#ffeead", "#ff6f69", "#ffcc5c", "#88d8b0"}))
var hatColor = rgb(g.pickOne([]string{"#cc6192", "#2663a3", "#a62116", "#3d8a6b", "#614f8a"}))
var mood = ""
if mood == "" {
@ -522,15 +525,15 @@ func femaleAvatar(seed uint64) string {
}, "")
m := []string{
"${skinColor}", skinColor,
"${hairColor}", hairColor,
"${eyesColor}", eyesColor,
"${eyebrowsColor}", eyebrowsColor,
"${accessoriesColor}", accessoriesColor,
"${mouthColor}", mouthColor,
"${glassesColor}", glassesColor,
"${clothesColor}", clothesColor,
"${hatColor}", hatColor,
"${skinColor}", skinColor.html(),
"${hairColor}", hairColor.html(),
"${eyesColor}", eyesColor.html(),
"${eyebrowsColor}", eyebrowsColor.html(),
"${accessoriesColor}", accessoriesColor.html(),
"${mouthColor}", mouthColor.html(),
"${glassesColor}", glassesColor.html(),
"${clothesColor}", clothesColor.html(),
"${hatColor}", hatColor.html(),
}
return strings.NewReplacer(m...).Replace(s)
}