diff options
author | Mark Kriegsman <kriegsman@tr.org> | 2014-10-14 20:13:52 +0400 |
---|---|---|
committer | Mark Kriegsman <kriegsman@tr.org> | 2014-10-14 20:13:52 +0400 |
commit | 3e312c671122164efd41a679eb58c8950222a611 (patch) | |
tree | c96fba79ed937e14b893cafd72c4e5b856e2742d | |
parent | 5b2c7cfa635c264a8b8823535d7b916735dda5ef (diff) |
Fix for #79, improve HSV ramps to/from pure black/white.
-rw-r--r-- | colorutils.cpp | 30 | ||||
-rw-r--r-- | colorutils.h | 16 |
2 files changed, 44 insertions, 2 deletions
diff --git a/colorutils.cpp b/colorutils.cpp index 781651a8..995db69e 100644 --- a/colorutils.cpp +++ b/colorutils.cpp @@ -83,12 +83,16 @@ void fill_gradient_RGB( CRGB* leds, bdistance87 = (endcolor.b - startcolor.b) << 7; uint16_t pixeldistance = endpos - startpos; - uint16_t p2 = pixeldistance / 2; - int16_t divisor = p2 ? p2 : 1; + int16_t divisor = pixeldistance ? pixeldistance : 1; + saccum87 rdelta87 = rdistance87 / divisor; saccum87 gdelta87 = gdistance87 / divisor; saccum87 bdelta87 = bdistance87 / divisor; + rdelta87 *= 2; + gdelta87 *= 2; + bdelta87 *= 2; + accum88 r88 = startcolor.r << 8; accum88 g88 = startcolor.g << 8; accum88 b88 = startcolor.b << 8; @@ -485,6 +489,28 @@ CHSV ColorFromPalette( const struct CHSVPalette16& pal, uint8_t index, uint8_t b uint8_t sat2 = entry->sat; uint8_t val2 = entry->val; + // Now some special casing for blending to or from + // either black or white. Black and white don't have + // proper 'hue' of their own, so when ramping from + // something else to/from black/white, we set the 'hue' + // of the black/white color to be the same as the hue + // of the other color, so that you get the expected + // brightness or saturation ramp, with hue staying + // constant: + + // If we are starting from white (sat=0) + // or black (val=0), adopt the target hue. + if( sat1 == 0 || val1 == 0) { + hue1 = hue2; + } + + // If we are ending at white (sat=0) + // or black (val=0), adopt the starting hue. + if( sat2 == 0 || val2 == 0) { + hue2 = hue1; + } + + sat1 = scale8_LEAVING_R1_DIRTY( sat1, f1); val1 = scale8_LEAVING_R1_DIRTY( val1, f1); diff --git a/colorutils.h b/colorutils.h index 1a3a8d85..3ca9829a 100644 --- a/colorutils.h +++ b/colorutils.h @@ -72,6 +72,22 @@ void fill_gradient( T* targetArray, endpos = startpos; } + // If we're fading toward black (val=0) or white (sat=0), + // then set the endhue to the starthue. + // This lets us ramp smoothly to black or white, regardless + // of what 'hue' was set in the endcolor (since it doesn't matter) + if( endcolor.value == 0 || endcolor.saturation == 0) { + endcolor.hue = startcolor.hue; + } + + // Similarly, if we're fading in from black (val=0) or white (sat=0) + // then set the starthue to the endhue. + // This lets us ramp smoothly up from black or white, regardless + // of what 'hue' was set in the startcolor (since it doesn't matter) + if( statcolor.value == 0 || startcolor.saturation == 0) { + startcolor.hue = endcolor.hue; + } + saccum87 huedistance87; saccum87 satdistance87; saccum87 valdistance87; |