class Rothko::Drawing

Attributes

img[RW]
palette[R]
width[RW]

Public Class Methods

new(path, width) click to toggle source

Takes in the path to the PNG file and the width (in 'pixels' - equivalent to two spaces)

# File lib/rothko.rb, line 37
def initialize(path, width)
  input = ChunkyPNG::Image.from_file(path)
  @width = width
  @img = input.resample_nearest_neighbor(self.width, get_height(input))
  make_drawing
end

Public Instance Methods

create_color_string() click to toggle source

Iterates over each pixel of resized image to find closest color

# File lib/rothko.rb, line 57
def create_color_string
  (0...img.height).map do |y|
    (0...img.width).map do |x|
      pix = self.img[x,y]
      pix_vals = [r(pix), g(pix), b(pix)]
      find_closest_term_color(pix_vals)
    end
  end.join
end
draw_line(pixels) click to toggle source

Takes in a string of colors and puts them out as background colored spaces For example, “rGK” creates a light_red square, a green square, and a black square

# File lib/rothko.rb, line 91
def draw_line(pixels)
  pix_line = ""
  pixels.each do |pixel|
    pix_line = pix_line + "  ".colorize(:background => find_color(pixel))
  end
  puts pix_line
end
find_closest_term_color(pixel_values) click to toggle source

Iterates over the palette to find the most similar color

# File lib/rothko.rb, line 68
def find_closest_term_color(pixel_values)
  color = ""
  lil_dist = 195075
  @@palette.each do |col_name, col_values|
    dist = find_distance(col_values, pixel_values)
    if dist < lil_dist
      lil_dist = dist
      color = col_name
    end
  end
  color
end
find_color(letter) click to toggle source

Matches letters to colors

# File lib/rothko.rb, line 100
def find_color(letter)
  case letter
  when "R"
    :red
  when "r"
    :light_red
  when "B"
    :blue
  when "b"
    :light_blue
  when "C"
    :cyan
  when "c"
    :light_cyan
  when "Y"
    :yellow
  when "y"
    :light_yellow
  when "G"
    :green
  when "g"
    :light_green
  when "K"
    :black
  when "k"
    :light_black
  when "W"
    :white
  when "w"
    :light_white
  when "M"
    :magenta
  when "m"
    :light_magenta
  else
    :white
  end
end
find_distance(color1, color2) click to toggle source

Helprt method

# File lib/rothko.rb, line 82
def find_distance(color1, color2)
  delta_r = color1[0] - color2[0];
  delta_g = color1[1] - color2[1];
  delta_b = color1[2] - color2[2];
  delta_r * delta_r + delta_g * delta_g + delta_b * delta_b
end
get_height(img) click to toggle source

Finds height of the image relative to provided width

# File lib/rothko.rb, line 52
def get_height(img)
  new_height = (img.height / (img.width.to_f / self.width.to_f)).ceil
end
make_drawing() click to toggle source

Calls create_color_string to get an array of colors & split it into rows Then iterates over that array, calling draw_line on each row

# File lib/rothko.rb, line 46
def make_drawing
  ln_arr = create_color_string.scan(/.{#{self.width}}/)
  ln_arr.each {|ln| draw_line(ln.split(""))}
end