def ColorFromPalette(pal, pos, brightness=255, blend=False): """ Approximates the FastLED ColorFromPalette() function ACCEPTS: color palette (list of CRGB, CSHV and/or packed ints), palette index (x16) + blend factor of next index (0-15) -- e.g. pass 32 to retrieve palette index 2, or 40 for an interpolated value between palette index 2 and 3, optional brightness (0-255), optional blend flag (True/False) RETURNS: CRGB color, no gamma correction """ # Alter 'pos' from FastLED-like behavior to fancyled range if blend: # Continuous interpolation 0.0 to 1.0 pos = (pos / 16.0) / len(pal) else: # No blending -- quantize to nearest palette bin pos = floor(pos / 16.0) / len(pal) color = fancy.palette_lookup(pal, pos) if brightness < 1.0: brightness /= 255.0 if isinstance(color, fancy.CHSV): color = fancy.CRGB(color) elif isinstance(color, int): color = fancy.unpack(color) color.red *= brightness color.green *= brightness color.blue *= brightness return color
def hsv2rgb_spectrum(hue, sat, val): """ This is named the same thing as FastLED's simpler HSV to RGB function (spectrum, vs rainbow) but implementation is a bit different for the sake of getting something running (adapted from some NeoPixel code). ACCEPTS: hue, saturation, value in range 0 to 255 RETURNS: CRGB color. """ return fancy.CRGB(fancy.CHSV(hue / 255, sat / 255, val / 255))
def loadDynamicGradientPalette(src, size): """ Kindasorta like FastLED's loadDynamicGradientPalette() function, with some gotchas. ACCEPTS: Gradient palette data as a 'bytes' type (makes it easier to copy over gradient palettes from existing FastLED Arduino sketches)... each palette entry is four bytes: a relative position (0-255) within the overall resulting palette (whatever its size), and 3 values for R, G and B...and a length for a new palette list to be allocated. RETURNS: list of CRGB colors. """ # Convert gradient from bytelist (groups of 4) to list of tuples, # each consisting of a position (0.0 to 1.0) and CRGB color. # (This is what FancyLED's expand_gradient needs for input.) grad = [] for i in range(0, len(src), 4): grad.append((src[i] / 255.0, fancy.CRGB(src[i+1], src[i+2], src[i+3]))) # Create palette (CRGB list) matching 'size' length return fancy.expand_gradient(grad, size)