darkerThan

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

24
main.go
View File

@ -126,20 +126,20 @@ func (c *RGB) to_hsv() *HSV {
h = 0.0 h = 0.0
if min != v { if min != v {
if v == r { if v == r {
h = math.Mod((g - b) / C, 6.0) h = math.Mod((g-b)/C, 6.0)
} }
if v == g { if v == g {
h = (b - r) / C + 2.0 h = (b-r)/C + 2.0
} }
if v == b { if v == b {
h = (r - g) / C + 4.0 h = (r-g)/C + 4.0
} }
h *= 60.0 h *= 60.0
if h < 0.0 { if h < 0.0 {
h += 360.0 h += 360.0
} }
} }
return f2hsv(h, s, v) return f2hsv(h, s, v)
} }
func (c *HSV) to_rgb() *RGB { func (c *HSV) to_rgb() *RGB {
@ -189,10 +189,18 @@ func (c *RGB) brighterOrDarkerThan(ref RGB, delta uint8) string {
} }
func (c *RGB) darkerThan(ref RGB, delta uint8) string { func (c *RGB) darkerThan(ref RGB, delta uint8) string {
/* XXX FIXME: THIS IS NOT CORRECT. The original implementation does darkening in HSV space. primary := c.to_hsv()
* cf. https://github.com/DiceBear/avatars/blob/master/packages/avatars/src/color.ts secondary := ref.to_hsv()
*/ delta_f := float64(delta) * 1.0 / 255.0
return fmt.Sprintf("#%02x%02x%02x", sub255(c.r, delta), sub255(c.g, delta), sub255(c.b, delta)) if primary.v <= secondary.v-delta_f {
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
}
primary.v = secondary.v - delta_f
if primary.v < 0 {
primary.v = 0
}
c = primary.to_rgb()
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
} }
func (c RGB) withAlpha(alpha float64) string { func (c RGB) withAlpha(alpha float64) string {