rothko 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rothko/version.rb +3 -0
  3. data/lib/rothko.rb +141 -0
  4. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8231ddb5dd94ffe8bdf9186b6185af753bb5fcccb950bbc0500261bc03029921
4
+ data.tar.gz: 580ac7d7094ae74e5fbc7c6c28098ab9aa0e1c35a6dfdd6dcb66dfe2871e13b1
5
+ SHA512:
6
+ metadata.gz: a1773bc329a54acb920063164c0808b05f2ea3497f497160c99f80a1753c68c445655ac77cd1d8853e26a8eb909e5543defbb94f8414d7b77b4ff3c5a5d5b65a
7
+ data.tar.gz: e427ea5aa5d0cefb3b3f44ddce4d13c577647cb8ed44aa91c3f41eb3fd070195050fed1e253fc2a35bf8c19c35fe707b8c8fe06969ec1211840aff63f8114312
@@ -0,0 +1,3 @@
1
+ module Rothko
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rothko.rb ADDED
@@ -0,0 +1,141 @@
1
+ require "rothko/version"
2
+ require "colorize"
3
+ require "chunky_png"
4
+
5
+ module Rothko
6
+
7
+ class Drawing
8
+
9
+ attr_accessor :img, :width
10
+ attr_reader :pallette
11
+
12
+ include ChunkyPNG::Color
13
+
14
+ # Pallette of default ANSI colors. Closest match is found from this hash,
15
+ # so these may be altered to produce a more faithful image or change colors.
16
+ @@pallette = {
17
+ "g" => [0,230,22],
18
+ "G" => [0,176,14],
19
+ "w" => [230,229,230],
20
+ "W" => [191,191,191],
21
+ "r" => [252,0,0],
22
+ "R" => [169,0,0],
23
+ "k" => [102,102,102],
24
+ "K" => [0,0,0],
25
+ "m" => [255,0,228],
26
+ "M" => [201,0,178],
27
+ "c" => [0,236,234],
28
+ "C" => [0,170,182],
29
+ "b" => [70,0,255],
30
+ "B" => [46,0,181],
31
+ "y" => [223,237,0],
32
+ "Y" => [149,158,0]
33
+ }
34
+
35
+ # Takes in the path to the PNG file and
36
+ # the width (in 'pixels' - equivalent to two spaces)
37
+ def initialize(path, width)
38
+ input = ChunkyPNG::Image.from_file(path)
39
+ @width = width
40
+ @img = input.resample_nearest_neighbor(self.width, get_height(input))
41
+ make_drawing
42
+ end
43
+
44
+ # Calls create_color_string to get an array of colors & split it into rows
45
+ # Then iterates over that array, calling draw_line on each row
46
+ def make_drawing
47
+ ln_arr = create_color_string.scan(/.{#{self.width}}/)
48
+ ln_arr.each {|ln| draw_line(ln.split(""))}
49
+ end
50
+
51
+ # Finds height of the image relative to provided width
52
+ def get_height(img)
53
+ new_height = img.height / (img.width / self.width)
54
+ end
55
+
56
+ # Iterates over each pixel of resized image to find closest color
57
+ def create_color_string
58
+ (0...img.height).map do |y|
59
+ (0...img.width).map do |x|
60
+ pix = self.img[x,y]
61
+ pix_vals = [r(pix), g(pix), b(pix)]
62
+ find_closest_term_color(pix_vals)
63
+ end
64
+ end.join
65
+ end
66
+
67
+ # Iterates over the pallette to find the most similar color
68
+ def find_closest_term_color(pixel_values)
69
+ color = ""
70
+ lil_dist = 195075
71
+ @@pallette.each do |col_name, col_values|
72
+ dist = find_distance(col_values, pixel_values)
73
+ if dist < lil_dist
74
+ lil_dist = dist
75
+ color = col_name
76
+ end
77
+ end
78
+ color
79
+ end
80
+
81
+ # Helprt method
82
+ def find_distance(color1, color2)
83
+ delta_r = color1[0] - color2[0];
84
+ delta_g = color1[1] - color2[1];
85
+ delta_b = color1[2] - color2[2];
86
+ delta_r * delta_r + delta_g * delta_g + delta_b * delta_b
87
+ end
88
+
89
+ # Takes in a string of colors and puts them out as background colored spaces
90
+ # For example, "rGK" creates a light_red square, a green square, and a black square
91
+ def draw_line(pixels)
92
+ pix_line = ""
93
+ pixels.each do |pixel|
94
+ pix_line = pix_line + " ".colorize(:background => find_color(pixel))
95
+ end
96
+ puts pix_line
97
+ end
98
+
99
+ # Matches letters to colors
100
+ def find_color(letter)
101
+ case letter
102
+ when "R"
103
+ :red
104
+ when "r"
105
+ :light_red
106
+ when "B"
107
+ :blue
108
+ when "b"
109
+ :light_blue
110
+ when "C"
111
+ :cyan
112
+ when "c"
113
+ :light_cyan
114
+ when "Y"
115
+ :yellow
116
+ when "y"
117
+ :light_yellow
118
+ when "G"
119
+ :green
120
+ when "g"
121
+ :light_green
122
+ when "K"
123
+ :black
124
+ when "k"
125
+ :light_black
126
+ when "W"
127
+ :white
128
+ when "w"
129
+ :light_white
130
+ when "M"
131
+ :magenta
132
+ when "m"
133
+ :light_magenta
134
+ else
135
+ :white
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rothko
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Brennan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides the 'Drawing' class which is initialized with a filepath and
14
+ a width. The image will be processed and put to the terminal as a collection of
15
+ ANSI BG colored squares.
16
+ email:
17
+ - ddbren@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/rothko.rb
23
+ - lib/rothko/version.rb
24
+ homepage: https://github.com/ddbrennan/rothko
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.7.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Convert PNG graphics into pixel art in the terminal
48
+ test_files: []