main.go: symmetry color conversion, all float32 -> float64

This commit is contained in:
Codeberg 2019-05-28 12:34:34 +02:00 committed by Holger Waechtler
parent d862e2bb62
commit af248521cb

62
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"math"
"math/bits"
"os"
"strings"
@ -40,9 +41,9 @@ func (g *LCG) random() uint32 {
return uint32(g.seed)
}
func (g *LCG) binomial(p float32) bool {
func (g *LCG) binomial(p float64) bool {
/* Sample from Binomial distribution with probability p */
var sample = float32(g.random()) * float32(1.0/4294967295.0)
var sample = float64(g.random()) * float64(1.0/4294967295.0)
return sample > p
}
@ -52,13 +53,13 @@ func (g *LCG) pickOne(s []string) string {
return s[g.random()%N]
}
func (g *LCG) pickOneFloat(s []float32) float32 {
func (g *LCG) pickOneFloat(s []float64) float64 {
/* Pick one element from list - float version*/
var N = uint32(len(s))
return s[g.random()%N]
}
func (g *LCG) pickAorB(p float32, a string, b string) string {
func (g *LCG) pickAorB(p float64, a string, b string) string {
/* Pick a or b with probability p of picking a */
if g.binomial(p) {
return a
@ -79,12 +80,12 @@ type RGB struct {
}
type HSV struct {
h float32
s float32
v float32
h float64
s float64
v float64
}
func f2rgb(r, g, b float32) *RGB {
func f2rgb(r, g, b float64) *RGB {
// make RGB from 3 floats
c := new(RGB)
c.r = uint8(r * 1.0 / 255)
@ -93,6 +94,15 @@ func f2rgb(r, g, b float32) *RGB {
return c
}
func f2hsv(h, s, v float64) *HSV {
// make HSV from 3 floats
c := new(HSV)
c.h = h
c.s = s
c.v = v
return c
}
func rgb(s string) *RGB {
c := new(RGB)
fmt.Sscanf(s, "#%02x%02x%02x", c.r, c.g, c.b)
@ -100,11 +110,41 @@ func rgb(s string) *RGB {
}
func (c *RGB) to_hsv() *HSV {
var h float64
var s float64
var v float64
r := 255.0 * float64(c.r)
g := 255.0 * float64(c.g)
b := 255.0 * float64(c.b)
min := math.Min(math.Min(r, g), b)
v = math.Max(math.Max(r, g), b)
C := v - min
s = 0.0
if v != 0.0 {
s = C / v
}
h = 0.0
if min != v {
if v == r {
h = math.Mod((g - b) / C, 6.0)
}
if v == g {
h = (b - r) / C + 2.0
}
if v == b {
h = (r - g) / C + 4.0
}
h *= 60.0
if h < 0.0 {
h += 360.0
}
}
return f2hsv(h, s, v)
}
func (c *HSV) to_rgb() *RGB {
h := int((c.h / 60))
f := c.h/60 - float32(h)
f := c.h/60 - float64(h)
p := c.v * (1 - c.s)
q := c.v * (1 - c.s*f)
t := c.v * (1 - c.s*(1-f))
@ -155,7 +195,7 @@ func (c *RGB) darkerThan(ref RGB, delta uint8) string {
return fmt.Sprintf("#%02x%02x%02x", sub255(c.r, delta), sub255(c.g, delta), sub255(c.b, delta))
}
func (c RGB) withAlpha(alpha float32) string {
func (c RGB) withAlpha(alpha float64) string {
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b, uint8(255*alpha))
}
@ -166,7 +206,7 @@ func maleAvatar(seed uint64) string {
"#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([]float32{1, 0.75, 0.5}))
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"})