Class: Rothko::Drawing
- Inherits:
-
Object
- Object
- Rothko::Drawing
- Includes:
- ChunkyPNG::Color
- Defined in:
- lib/rothko.rb
Constant Summary collapse
- @@palette =
Pallette of default ANSI colors. Closest match is found from this hash, so these may be altered to produce a more faithful image or change colors.
{ "g" => [0,230,22], "G" => [0,176,14], "w" => [230,229,230], "W" => [191,191,191], "r" => [252,0,0], "R" => [169,0,0], "k" => [102,102,102], "K" => [0,0,0], "m" => [255,0,228], "M" => [201,0,178], "c" => [0,236,234], "C" => [0,170,182], "b" => [70,0,255], "B" => [46,0,181], "y" => [223,237,0], "Y" => [149,158,0] }
Instance Attribute Summary collapse
-
#img ⇒ Object
Returns the value of attribute img.
-
#palette ⇒ Object
readonly
Returns the value of attribute palette.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#create_color_string ⇒ Object
Iterates over each pixel of resized image to find closest color.
-
#draw_line(pixels) ⇒ Object
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.
-
#find_closest_term_color(pixel_values) ⇒ Object
Iterates over the palette to find the most similar color.
-
#find_color(letter) ⇒ Object
Matches letters to colors.
-
#find_distance(color1, color2) ⇒ Object
Helprt method.
-
#get_height(img) ⇒ Object
Finds height of the image relative to provided width.
-
#initialize(path, width) ⇒ Drawing
constructor
Takes in the path to the PNG file and the width (in ‘pixels’ - equivalent to two spaces).
-
#make_drawing ⇒ Object
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.
Constructor Details
#initialize(path, width) ⇒ Drawing
Takes in the path to the PNG file and the width (in ‘pixels’ - equivalent to two spaces)
37 38 39 40 41 42 |
# 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 |
Instance Attribute Details
#img ⇒ Object
Returns the value of attribute img.
9 10 11 |
# File 'lib/rothko.rb', line 9 def img @img end |
#palette ⇒ Object (readonly)
Returns the value of attribute palette.
10 11 12 |
# File 'lib/rothko.rb', line 10 def palette @palette end |
#width ⇒ Object
Returns the value of attribute width.
9 10 11 |
# File 'lib/rothko.rb', line 9 def width @width end |
Instance Method Details
#create_color_string ⇒ Object
Iterates over each pixel of resized image to find closest color
57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
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
91 92 93 94 95 96 97 |
# 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) ⇒ Object
Iterates over the palette to find the most similar color
68 69 70 71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
Matches letters to colors
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# 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) ⇒ Object
Helprt method
82 83 84 85 86 87 |
# 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) ⇒ Object
Finds height of the image relative to provided width
52 53 54 |
# 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 ⇒ Object
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
46 47 48 49 |
# 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 |