fic: Pick HSL function to generate random colors
This commit is contained in:
parent
6395eaaf5f
commit
63de5d64b1
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -155,6 +156,10 @@ func createTeam(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if ut.Color == 0 {
|
||||
ut.Color = fic.HSL{rand.Float64(), 1, 0.5}.ToRGB()
|
||||
}
|
||||
|
||||
return fic.CreateTeam(strings.TrimSpace(ut.Name), ut.Color, ut.ExternalId)
|
||||
}
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ func treatRegistration(pathname string, team_id string) {
|
|||
} else if denyTeamCreation {
|
||||
log.Printf("%s [ERR] Registration received, whereas team creation denied. Skipped.\n", id)
|
||||
} else if validTeamName(nTeam.TeamName) {
|
||||
if team, err := fic.CreateTeam(nTeam.TeamName, uint32(rand.Int31n(16581376))); err != nil {
|
||||
if team, err := fic.CreateTeam(nTeam.TeamName, fic.HSL{H: rand.Float64(), L: 1, S: 0.5}.ToRGB(), ""); err != nil {
|
||||
log.Printf("%s [ERR] Unable to register new team %s: %s\n", id, nTeam.TeamName, err)
|
||||
} else {
|
||||
registrationProcess(id, team, nTeam.Members, team_id)
|
||||
|
|
|
@ -125,3 +125,57 @@ func Apr1Md5(password string, salt string) string {
|
|||
|
||||
return resultString
|
||||
}
|
||||
|
||||
// Function copied from https://github.com/gerow/go-color/blob/master/color.go
|
||||
type HSL struct {
|
||||
H, S, L float64
|
||||
}
|
||||
|
||||
func hueToRGB(v1, v2, h float64) uint32 {
|
||||
if h < 0 {
|
||||
h += 1
|
||||
}
|
||||
if h > 1 {
|
||||
h -= 1
|
||||
}
|
||||
var res float64
|
||||
switch {
|
||||
case 6*h < 1:
|
||||
res = (v1 + (v2-v1)*6*h)
|
||||
case 2*h < 1:
|
||||
res = v2
|
||||
case 3*h < 2:
|
||||
res = v1 + (v2-v1)*((2.0/3.0)-h)*6
|
||||
default:
|
||||
res = v1
|
||||
}
|
||||
return uint32((res + 1/512.0) * 255)
|
||||
|
||||
}
|
||||
|
||||
func (c HSL) ToRGB() (rgb uint32) {
|
||||
h := c.H
|
||||
s := c.S
|
||||
l := c.L
|
||||
|
||||
if s == 0 {
|
||||
// it's gray
|
||||
v := uint32((l + 1/512.0) * 255)
|
||||
return v*65536 + v*256 + v
|
||||
}
|
||||
|
||||
var v1, v2 float64
|
||||
if l < 0.5 {
|
||||
v2 = l * (1 + s)
|
||||
} else {
|
||||
v2 = (l + s) - (s * l)
|
||||
}
|
||||
|
||||
v1 = 2*l - v2
|
||||
|
||||
r := hueToRGB(v1, v2, h+(1.0/3.0))
|
||||
g := hueToRGB(v1, v2, h)
|
||||
b := hueToRGB(v1, v2, h-(1.0/3.0))
|
||||
|
||||
return r*65536 + g*256 + b
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue