module Piccolor

Constants

VERSION

Public Class Methods

check_greys(word, h, s, l, hex=nil) click to toggle source
# File lib/piccolor.rb, line 41
def self.check_greys(word, h, s, l, hex=nil)

  if (s + 50) ** 2 + (l - 50) ** 2 <= 45
    return "white"
  elsif l < -38
    return "black"
  elsif s < -35 and l <= -16
    return "dark grey"
  elsif s < -48 and l < 30 and l > -16
    return "grey"
  elsif s < -48 and l >= 30
    return "light grey"
  elsif (h < 76 or h > 345) and s < -25 and l > 0
    return "warm grey"
  elsif s < -25 and l > 0
    return "cold grey"
  else
    nil
  end
end
desc_correct(word, h, s, l, hex=nil) click to toggle source
# File lib/piccolor.rb, line 62
def self.desc_correct(word, h, s, l, hex=nil)
  # p "#{word}, #{h}, #{s}, #{l}, #{hex}"

  grey = self.check_greys(word, h, s, l, hex)
  return grey if not grey.nil?

  if l >= -15 and l <= 15
    self.doubles word
  elsif l < -15
    self.word_correct "dark #{word}"
  elsif l > 15
    self.word_correct "light #{word}"
  end
end
doubles(phrase) click to toggle source
# File lib/piccolor.rb, line 77
def self.doubles(phrase)
  phrase.gsub("dark light ", "").gsub("light dark ", "")
    .gsub("light light ", "light ").gsub("dark dark ", "dark ")
end
full_colors() click to toggle source
# File lib/piccolor.rb, line 91
def self.full_colors
  regexp = /\A\[(\d{1,3}), (\d{1,3}), (\d{1,3})\] ([a-z\s]+)\z/
  colors = {}

  self.get_text_file.each do |line|

    matched = line.match(regexp)
    rgb = Color::RGB.new(matched[1].to_i, matched[2].to_i, matched[3].to_i).html()
    human = matched[4].gsub("\n", "")

    colors[rgb] = human
  end

  colors
end
get_text_file() click to toggle source
# File lib/piccolor.rb, line 107
def self.get_text_file
  open(File.dirname(__FILE__) + "/piccolor/xkcd.txt").readlines
end
to_human(hex) click to toggle source
# File lib/piccolor.rb, line 21
def self.to_human(hex)
  correct_rgb = Color::RGB.from_html(hex)
  correct_hex = correct_rgb.html
  if @@colors.nil?
    @@colors = self.full_colors
  end

  if not @@colors[correct_hex].nil?
    # yep, this is your colour
    @@colors[correct_hex]
  else
    # okay, some cleverness now
    correct_hsl = correct_rgb.to_hsl
    corrected_hsl = correct_hsl.clone
    corrected_hsl.saturation = 100
    corrected_hsl.lightness = 50
    self.desc_correct(@@colors[corrected_hsl.html], correct_hsl.hue, correct_hsl.saturation - 50, correct_hsl.lightness - 50, correct_hex)
  end
end
word_correct(phrase) click to toggle source
# File lib/piccolor.rb, line 82
def self.word_correct(phrase)
  phrase = self.doubles(phrase)
  if @@corrections.has_key?(phrase)
    @@corrections[phrase]
  else
    phrase
  end
end