import color from random import pick palette_meta = @type: 'Palette' # The number of colors in the palette @size: || size self.colors @meta last_index: || (size self.colors)- 1 # Picks a color from the palette at random @meta pick: || pick self.colors # Gets a color from the palette, non-integer values produce a faded result @meta get: |x| colors = self.colors match (colors.get x.floor()), (colors.get x.ceil()) first, null then copy first null, last then copy last a, b then a.mix b, x % 1 # Maps 0-1 to an interpolated color from the list of colors in the palette @meta fade: |x| self.get x * self.last_index() export make_palette = |colors...| result = {colors: colors.to_tuple()} result.with_meta palette_meta @tests = @test palette_fade: || red = color 1, 0, 0 green = color 0, 1, 0 blue = color 0, 0, 1 p = make_palette red, green, blue assert_eq p.fade(0), red assert_eq p.fade(0.5), green assert_eq p.fade(1), blue assert_eq p.fade(0.25), color(0.5, 0.5, 0) assert_eq p.fade(0.75), color(0.0, 0.5, 0.5)