From 5fffca7b9c00bcd18624f4e533481201dd99735b Mon Sep 17 00:00:00 2001 From: Codeberg Date: Tue, 28 May 2019 13:08:54 +0200 Subject: [PATCH] brighterThan --- main.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 5677d3e..4376585 100644 --- a/main.go +++ b/main.go @@ -166,26 +166,19 @@ func (c *HSV) to_rgb() *RGB { return f2rgb(0, 0, 0) } -func add255(x uint8, y uint8) uint8 { - if x < 255-y { - return x + y +func (c *RGB) brighterThan(ref RGB, delta uint8) string { + primary := c.to_hsv() + 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 -} - -func sub255(x uint8, y uint8) uint8 { - if x > y { - return x - y + primary.v = secondary.v + delta_f + if primary.v > 360 { + primary.v = 360 } - return 0 -} - -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) + c = primary.to_rgb() + return fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b) } 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) } +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 { return fmt.Sprintf("#%02x%02x%02x%02x", c.r, c.g, c.b, uint8(255*alpha)) }