module Tiedye

Constants

COLORS
HEX_DIGIT
VERSION

Public Class Methods

all_colors() click to toggle source
# File lib/tiedye.rb, line 36
def self.all_colors
  return COLORS.keys
end
method_missing(name, *args) click to toggle source
# File lib/tiedye.rb, line 30
def self.method_missing(name, *args)
  return COLORS[name.to_s.downcase] if COLORS[name.to_s.downcase]
  return COLORS[name.to_s] if COLORS[name.to_s]
  return name
end
to_hex(r = "x", g = "x", b = "x") click to toggle source
# File lib/tiedye.rb, line 18
def self.to_hex(r = "x", g = "x", b = "x")
  if r.class == Array
    temp = r
    r = temp[0]
    g = temp[1]
    b = temp[2]
  end
  return random_hex if invalid(r, g, b)
  r, g, b = rgb_float_to_int(r, g, b)
  return "#" + hex_digit(r) + hex_digit(g) + hex_digit(b)
end
to_rgb(hex) click to toggle source
# File lib/tiedye.rb, line 4
def self.to_rgb(hex)
  hex = hex.gsub("#", "")
  hex = [hex[0..1], hex[2..3], hex[4..5]]
  options = {"FF" => 255, "00" => 0}
  return hex.collect{|h| options[h]}
  # hex = to_6_hex(hex)
  # hex_pairs = hex.scan(/.{1,2}/)
  # rgb = []
  # rgb[0] = hex_pair_to_rgb_digit(hex_pairs[0])
  # rgb[1] = hex_pair_to_rgb_digit(hex_pairs[1])
  # rgb[2] = hex_pair_to_rgb_digit(hex_pairs[2])
  # return rgb
end

Private Class Methods

hex_digit(digit) click to toggle source
# File lib/tiedye.rb, line 68
def self.hex_digit(digit)
  return HEX_DIGIT[digit / 16] + HEX_DIGIT[digit % 16]
end
hex_pair_to_rgb_digit(hex_pair) click to toggle source
# File lib/tiedye.rb, line 41
def self.hex_pair_to_rgb_digit(hex_pair)
  hexz = hex_pair.upcase.split("")
  number  = HEX_DIGIT.key(hexz[0]) * 16
  number += HEX_DIGIT.key(hexz[1])
  return number
end
invalid(r, g, b) click to toggle source
# File lib/tiedye.rb, line 81
def self.invalid(r, g, b)
  begin
    [r, g, b].each do |x|
      return true if x == "x"
      return true if x > 255 || x < 0
    end
  rescue
    return false
  end
  return false
end
random_hex() click to toggle source
# File lib/tiedye.rb, line 60
def self.random_hex
  hex = "#"
  3.times do
    hex += hex_digit(rand(0..255))
  end
  return hex
end
rgb_float_to_int(r, g, b) click to toggle source
# File lib/tiedye.rb, line 72
def self.rgb_float_to_int(r, g, b)
  if r.class == Float && r <= 1 && g <= 1 && b <= 1
    r = (r * 255).to_i
    g = (g * 255).to_i
    b = (b * 255).to_i
  end
  return [r, g, b]
end
to_6_hex(hex) click to toggle source
# File lib/tiedye.rb, line 48
def self.to_6_hex(hex)
  hex = hex.gsub("#", "")
  if hex.length == 3
    letters = hex.split("")
    hex = ""
    letters.each do |letter|
      hex += letter + letter
    end
  end
  return hex
end