brighterThan

This commit is contained in:
Codeberg 2019-05-28 13:08:54 +02:00 committed by Holger Waechtler
parent 88ab3449de
commit 5fffca7b9c

40
main.go
View File

@ -166,26 +166,19 @@ func (c *HSV) to_rgb() *RGB {
return f2rgb(0, 0, 0) return f2rgb(0, 0, 0)
} }
func add255(x uint8, y uint8) uint8 { func (c *RGB) brighterThan(ref RGB, delta uint8) string {
if x < 255-y { primary := c.to_hsv()
return x + y 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)
} }
return 255 primary.v = secondary.v + delta_f
} if primary.v > 360 {
primary.v = 360
func sub255(x uint8, y uint8) uint8 {
if x > y {
return x - y
} }
return 0 c = primary.to_rgb()
} return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
func (c *RGB) brighterOrDarkerThan(ref RGB, delta uint8) string {
/* XXX FIXME: THIS IS PROBABLY NOT CORRECT. Need to look into original implementation. */
if true {
return fmt.Sprintf("#%02x%02x%02x", add255(c.r, delta), add255(c.g, delta), add255(c.b, delta))
}
return ref.darkerThan(ref, delta)
} }
func (c *RGB) darkerThan(ref RGB, delta uint8) string { func (c *RGB) darkerThan(ref RGB, delta uint8) string {
@ -203,6 +196,17 @@ func (c *RGB) darkerThan(ref RGB, delta uint8) string {
return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b) return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
} }
func (c *RGB) brighterOrDarkerThan(ref RGB, delta uint8) string {
primary := c.to_hsv()
secondary := ref.to_hsv()
if primary.v <= secondary.v {
return c.darkerThan(ref, delta)
} else {
return c.brighterThan(ref, delta)
}
}
func (c RGB) withAlpha(alpha float64) string { func (c RGB) withAlpha(alpha float64) string {
return fmt.Sprintf("#%02x%02x%02x%02x", c.r, c.g, c.b, uint8(255*alpha)) return fmt.Sprintf("#%02x%02x%02x%02x", c.r, c.g, c.b, uint8(255*alpha))
} }