Created
July 24, 2015 22:49
-
-
Save mamuso/8922bf67a95ad27d6d42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'miro' | |
require 'color' | |
require 'color/rgb/contrast' | |
Miro.options[:color_count] = 4 | |
Miro.options[:method] = 'histogram' | |
class Colorify | |
def get(img) | |
base_color = nil | |
final_color = nil | |
hue = nil | |
white = Color::RGB.new 255,255,255 | |
lightness_base = 70 | |
colors = Miro::DominantColors.new(img) | |
colors_hsl = colors.to_hsl | |
# Trying to exclude as many brown colors as possible | |
colors_hsl.each do |c| | |
base_color ||= (c[0]*360).between?(10, 50) ? nil : c | |
end | |
# Nothink works? no problem, let's try the second most prominent color | |
base_color ||= colors_hsl[1] | |
# Before committing to a particular hue, let's check if the color is too dark | |
hue = (base_color[1] < 0.1 && base_color[0] < 0.005) ? rand(0..360)/360.00 : base_color[0] | |
# Now let's generate an accessible color | |
lightness_base.downto(1) do |lightness| | |
final_color = Color::HSL.new hue*360, 50, lightness | |
break if final_color.to_rgb.contrast(white) > 0.47 | |
end | |
return final_color.to_rgb.hex.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment