role color stuff
This commit is contained in:
parent
41d48bf60a
commit
f72c7a357b
9 changed files with 165 additions and 14 deletions
|
@ -1,13 +1,18 @@
|
|||
package utils
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
Pred float64 = 0.299
|
||||
Pgreen float64 = 0.587
|
||||
Pblue float64 = 0.114
|
||||
)
|
||||
|
||||
func IsDarkColor(r, g, b uint8) bool {
|
||||
rC := 0.299 * math.Pow(float64(r), 2)
|
||||
gC := 0.587 * math.Pow(float64(g), 2)
|
||||
bC := 0.114 * math.Pow(float64(b), 2)
|
||||
|
||||
lum := math.Sqrt(rC + gC + bC)
|
||||
lum := Luminance(r, g, b)
|
||||
|
||||
return lum <= 130
|
||||
}
|
||||
|
@ -19,3 +24,47 @@ func IntToRgb(color uint32) (uint8, uint8, uint8) {
|
|||
|
||||
return uint8(r), uint8(g), uint8(b)
|
||||
}
|
||||
|
||||
func RgbToString(r, g, b uint8) string {
|
||||
return fmt.Sprintf("#%02x%02x%02x", r, g, b)
|
||||
}
|
||||
|
||||
func AltColor(r, g, b uint8) (uint8, uint8, uint8) {
|
||||
brightnessAmount := -0.6
|
||||
if IsDarkColor(r, g, b) {
|
||||
brightnessAmount = 0.85
|
||||
}
|
||||
return Brighten(r, g, b, brightnessAmount)
|
||||
}
|
||||
|
||||
func Brighten(r, g, b uint8, amount float64) (uint8, uint8, uint8) {
|
||||
return multiply(r, amount), multiply(g, amount), multiply(b, amount)
|
||||
}
|
||||
|
||||
func multiply(i uint8, amount float64) uint8 {
|
||||
iF := float64(i)
|
||||
return uint8(
|
||||
math.Max(
|
||||
0,
|
||||
math.Min(
|
||||
255, iF+255*amount,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func Luminance(r, g, b uint8) float64 {
|
||||
rC := Pred * math.Pow(float64(r), 2)
|
||||
gC := Pgreen * math.Pow(float64(g), 2)
|
||||
bC := Pblue * math.Pow(float64(b), 2)
|
||||
|
||||
return math.Sqrt(rC + gC + bC)
|
||||
}
|
||||
|
||||
func WCAGRatio(l1, l2 float64) float64 {
|
||||
if l1 < l2 {
|
||||
return (l1 + 0.05) / (l2 + 0.05)
|
||||
} else {
|
||||
return (l2 + 0.05) / (l1 + 0.05)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,11 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
WCAGAAA float64 = 0.14285714285714285
|
||||
WCAGAA float64 = 0.25
|
||||
)
|
||||
|
||||
func TestIntToRgb(t *testing.T) {
|
||||
r, g, b := utils.IntToRgb(0x123456)
|
||||
assert.Equal(t, uint8(0x12), r, "red")
|
||||
|
@ -27,3 +32,39 @@ func TestIsDarkColor(t *testing.T) {
|
|||
isReallyQuestionable := utils.IsDarkColor(0x00, 0x88, 0x00)
|
||||
assert.True(t, isReallyQuestionable)
|
||||
}
|
||||
|
||||
func TestBrighten(t *testing.T) {
|
||||
r, g, b := utils.Brighten(0, 0, 0, 0.1)
|
||||
assert.Equal(t, uint8(0x19), r)
|
||||
assert.Equal(t, uint8(0x19), g)
|
||||
assert.Equal(t, uint8(0x19), b)
|
||||
// assert.LessOrEqual(t, WCAGAA, utils.WCAGRatio(
|
||||
// utils.Luminance(0, 0, 0),
|
||||
// utils.Luminance(r, g, b),
|
||||
// ))
|
||||
|
||||
r, g, b = utils.Brighten(0x88, 0x88, 0x88, -0.1)
|
||||
assert.Equal(t, uint8(0x88-0x19-1), r)
|
||||
assert.Equal(t, uint8(0x88-0x19-1), g)
|
||||
assert.Equal(t, uint8(0x88-0x19-1), b)
|
||||
// assert.LessOrEqual(t, WCAGAA, utils.WCAGRatio(
|
||||
// utils.Luminance(0x88, 0x88, 0x88),
|
||||
// utils.Luminance(r, g, b),
|
||||
// ))
|
||||
}
|
||||
|
||||
func TestRgbToString(t *testing.T) {
|
||||
assert.Equal(t, "#acab69", utils.RgbToString(0xac, 0xab, 0x69))
|
||||
}
|
||||
|
||||
func TestAltColor(t *testing.T) {
|
||||
r, g, b := utils.AltColor(0xa2, 0xc2, 0x42)
|
||||
assert.Equal(t, uint8(0x09), r, "red")
|
||||
assert.Equal(t, uint8(0x29), g, "green")
|
||||
assert.Equal(t, uint8(0x00), b, "blue")
|
||||
|
||||
r, g, b = utils.AltColor(0xa2, 0x15, 0x18)
|
||||
assert.Equal(t, uint8(0xff), r, "red")
|
||||
assert.Equal(t, uint8(0xed), g, "green")
|
||||
assert.Equal(t, uint8(0xf0), b, "blue")
|
||||
}
|
||||
|
|
15
utils/emojis_test.go
Normal file
15
utils/emojis_test.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRandomEmoji(t *testing.T) {
|
||||
for i := len(utils.AllEmojis) * 1000; i >= 0; i-- {
|
||||
e := utils.GetRandomEmoji()
|
||||
assert.Containsf(t, e.Name, "roleypolynext", "not valid on iteration %d", i)
|
||||
}
|
||||
}
|
15
utils/template_fns.go
Normal file
15
utils/template_fns.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/types"
|
||||
)
|
||||
|
||||
func RoleInputID(c *types.Category, r *types.Role) string {
|
||||
return fmt.Sprintf("category-%s_role-%s", c.ID, r.ID)
|
||||
}
|
||||
|
||||
func RoleInputName(c *types.Category) string {
|
||||
return fmt.Sprintf("category_group_%s", c.ID)
|
||||
}
|
17
utils/template_fns_test.go
Normal file
17
utils/template_fns_test.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/types"
|
||||
"git.sapphic.engineer/roleypoly/v4/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRoleInputID(t *testing.T) {
|
||||
assert.Equal(t, "category-123_role-456", utils.RoleInputID(&types.Category{ID: "123"}, &types.Role{ID: "456"}))
|
||||
}
|
||||
|
||||
func TestRoleInputName(t *testing.T) {
|
||||
assert.Equal(t, "category_group_123", utils.RoleInputName(&types.Category{ID: "123"}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue