Skip to content

Instantly share code, notes, and snippets.

@lacostenycoder
Last active April 26, 2018 01:55
Show Gist options
  • Save lacostenycoder/394341d3c63589a050d72b8d9ae529aa to your computer and use it in GitHub Desktop.
Save lacostenycoder/394341d3c63589a050d72b8d9ae529aa to your computer and use it in GitHub Desktop.
web hex colors to array
require 'paint'
#intantiate web_colors from gist and shuffle them
eval(`curl https://gist.githubusercontent.com/lacostenycoder/394341d3c63589a050d72b8d9ae529aa/raw/4a11cd6ca81e07a643e5f3481e38494ff3c9b973/web_colors_shuffle.rb`)
def hex_to_rgb(hex)
hex.gsub!("#","")
/(?<r>..)(?<g>..)(?<b>..)/ =~ hex
[r,g,b].map {|cs| cs.to_i(16)}
end
def rgb_to_hsl(r,g,b, by_hue=false)
# normalize r, g and b
r /= 255.0
g /= 255.0
b /= 255.0
c_min = [r,g,b].min
c_max = [r,g,b].max
h = s = l = (c_min + c_max) / 2
d = (c_max - c_min)#.to_f
# compute hue
if c_max == c_min
h = s = 0.0 # achromatic i.e. ffffff or 000000
else
s = l > 0.5 ? d / (2 - c_max - c_min) : d / (c_max + c_min);
h = (g - b) / d + ( g < b ? 6 : 0) if c_max == r
h = (b - r) / d + 2 if c_max == g if c_max == g
h = (r - g) / d + 4 if c_max == b if c_max == b
end
h /= 6
by_hue ? h : [h, s, l]
end
def sort_em(colors, by_hue=false)
colors.sort_by { |color| rgb_to_hsl(*hex_to_rgb(color), by_hue) }
end
def puts_em(colors)
colors.each{|color| puts Paint[color, color]}
end
#shuffled web_colors sorted by hue
puts "\nShuffled web_colors by hue \n\n"
puts_em(sort_em @shuffled, by_hue=true)
puts "\nShuffled web_colors by HSL \n\n"
#shuffled web_colors sorted by HLS
puts_em(@shuffled)
# sample uniformly at random from RGB space
colors = 200.times.map { (0..255).to_a.sample(3).map { |i| i.to_s(16).rjust(2, '0')}.join }
puts "\n Shuffled Random By Hue\n\n"
puts_em(sort_em colors, by_hue=true)
puts "\nShuffled Random by HSL\n\n"
puts_em(sort_em colors)
@web_colors = %w[000000
FFFFFF
FF00FF
0000FF
00FFFF
00FF00
FFFF00
FF0000
7F7F7F
FF007F
FF7FFF
7F007F
7F00FF
7F7FFF
00007F
007FFF
7FFFFF
007F7F
00FF7F
7FFF7F
007F00
7FFF00
7F7F00
FF7F00
FF7F7F
7F0000
AA0055
AA5555
550000
3FFF00
FF00CC
990066
9966CC
CCCCFF
9999CC
003399
003366
006633
00CC99
00CCCC
FFCC99
FF9966
FF6600
990000
6D9292
DFFF00
FFD800
CC8899
DD00FF
EEEEFF
DDFFEE
EEFFBB
EECC00
CC7722
FFFFF0
D2691E
964B00
009000
FFC0CB
997A8D
002FA7
F0F8FF
4B5320
00A693
FFF8E7].map{|c| "##{c}"}
@shuffled = @web_colors.shuffle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment