module RainbowColors class Scheme def self.analogous(base_color) secondary = color_with_variance 30, base_color tertiary = color_with_variance -30, base_color [base_color, secondary, tertiary] end def self.complementary(base_color) secondary = color_with_variance 180, base_color [base_color, secondary] end def self.complementary_split(base_color) secondary = color_with_variance 150, base_color tertiary = color_with_variance 210, base_color [base_color, secondary, tertiary] end def self.triad(base_color) secondary = color_with_variance 120, base_color tertiary = color_with_variance -120, base_color [base_color, secondary, tertiary] end def self.tints(base_color) shades_tints "#FFFFFF", base_color end def self.shades(base_color) shades_tints "#000000", base_color end def self.harmonious_grays(base_color) weight = 90 colors = [] colors << mix("#424242", base_color, weight) colors << mix("#FAFAFA", base_color, weight) colors end private private_class_method def self.color_with_variance(variance, base_color) rgb = RainbowColors::Convertor.rgb_from_hex base_color hsl = RainbowColors::Convertor.hsl_from_rgb rgb color = RainbowColors::Convertor.rgb_from_hsl({ hue: hsl[:hue] + variance, saturation: hsl[:saturation], luminance: hsl[:luminance] }) RainbowColors::Convertor.hex_from_rgb color end # Algorithm shamelessly stolen from SASS: # https://github.com/sass/sass/blob/stable/lib/sass/script/functions.rb#L1313 private_class_method def self.mix(color1, color2, weight) color1 = RainbowColors::Convertor.rgb_from_hex color1 color2 = RainbowColors::Convertor.rgb_from_hex color2 p = weight / 100.0 w = p * 2 - 1 w1 = (w + 1) / 2 w2 = 1 - w1 r = (color1[:red] * w1 + color2[:red] * w2).round g = (color1[:green] * w1 + color2[:green] * w2).round b = (color1[:blue] * w1 + color2[:blue] * w2).round RainbowColors::Convertor.hex_from_rgb({ red: r, green: g, blue: b }) end private_class_method def self.shades_tints(mix_color, base_color) colors = [base_color] weight = 0 increment = 15 5.times do weight = weight + increment colors.push mix(mix_color, base_color, weight) end colors end end end