Created
April 12, 2016 19:35
-
-
Save FredrikAugust/62caacd502d32565faecc268c49ef478 to your computer and use it in GitHub Desktop.
Color thing wo/ GUI
This file contains hidden or 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
#!/home/greg/.rvm/rubies/default/bin/ruby | |
require 'rmagick' | |
# monkeypatch string to convert bases | |
class String | |
def convert_base(from, to) | |
to_i(from).to_s(to) | |
end | |
end | |
class Array | |
def mean | |
inject(&:+) / size # sum / length of self | |
end | |
end | |
# creation and showing the colored square | |
class Square | |
def initialize(color) | |
imgl = Magick::ImageList.new | |
imgl.new_image(200,200, Magick::HatchFill.new(color, color)) | |
imgl.write("square.png") | |
system 'feh "square.png"' | |
end | |
end | |
class Color | |
attr_reader :hex_color | |
attr_reader :rgb | |
def to_s | |
"#{@hex_color}" | |
end | |
def initialize(rgb_s) | |
@hex_color = rgb_s | |
# split into arrays of 2 | |
rgb_a = rgb_s.sub('#','').split('').each_slice(2).to_a | |
# convert from base16 to base10 | |
rgb_a.map! {|x| x.join('').convert_base(16,10).to_i} | |
@rgb = {r: rgb_a[0], g: rgb_a[1], b: rgb_a[2]} | |
end | |
def self.random_color | |
_rand_color = "#" + rand("ffffff".convert_base(16,10).to_i) | |
.to_s.convert_base(10,16) | |
_rand_color += "0" until _rand_color.length == 7 | |
_rand_color | |
end | |
end | |
class ColorGuesser | |
attr_accessor :light, :dark | |
def initialize | |
# instanciate empty arrs | |
@light, @dark = {r: [], g: [], b:[]}, {r: [], g: [], b:[]} | |
end | |
def add_color(color, type) | |
_color = Color.new(color).rgb | |
_new = instance_variable_get("@#{type}").merge(_color){ |k,o,n|o << n } | |
instance_variable_set("@#{type}", _new) | |
end | |
def get_seed_data | |
puts "Color will be shown, press 'q' when you have decided" | |
puts "l) light color\nd) dark color" | |
loop do | |
_color = Color.random_color | |
Square.new(_color) | |
case gets.chomp | |
when "l" | |
add_color(_color, :light) | |
next | |
when "d" | |
add_color(_color, :dark) | |
next | |
when "q" | |
break | |
else | |
next | |
end | |
break | |
end | |
end | |
def seed | |
puts "Enter empty line when done" | |
get_seed_data | |
end | |
def color_chance(color) | |
diff = Proc.new{ |val, target| (val - target).to_f.abs } | |
["dark", "light"].map do |t| | |
self.instance_variable_get("@#{t}").map do |k,v| # get current color | |
target_color = color.rgb["#{k}".to_sym] | |
diff.call(v.mean, target_color) | |
end.mean | |
end.zip([:dark, :light]).map(&:reverse).to_h | |
end | |
def decide(color) | |
puts "#{color} is " + color_chance(color).min_by{ |k,v| v }.first.to_s | |
end | |
def ask(color) | |
Square.new(color) | |
decide(Color.new(color)) | |
end | |
end | |
color_guesser = ColorGuesser.new | |
color_guesser.seed | |
puts "Try to ask for a color!" | |
loop do | |
case (_i = gets.chomp) | |
when "q" then break | |
else | |
color_guesser.ask(_i) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment