Class: Stitchifier
- Inherits:
-
Object
- Object
- Stitchifier
- Includes:
- Chroma, Magick, Miro
- Defined in:
- lib/stitchify.rb
Constant Summary collapse
- HSLA_BLACK =
[0, 0, 0, 1]
- HSLA_WHITE =
[0, 0, 100, 1]
- HSL_OPEN_CONST =
"hsl("
- FILLABLE_SHAPES =
[ 'sm_rectangle', 'triangle', 'circle', 'diamond', 'heart', 'star', 'reverse_triangle', 'left_triangle', 'right_triangle' ]
Instance Attribute Summary collapse
-
#base_pixel_arr ⇒ Object
Returns the value of attribute base_pixel_arr.
-
#color_set ⇒ Object
Returns the value of attribute color_set.
-
#dominant_colors ⇒ Object
Returns the value of attribute dominant_colors.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#img ⇒ Object
Returns the value of attribute img.
-
#img_path ⇒ Object
Returns the value of attribute img_path.
-
#num_of_colors ⇒ Object
Returns the value of attribute num_of_colors.
-
#num_of_off_colors ⇒ Object
Returns the value of attribute num_of_off_colors.
-
#pos_x ⇒ Object
Returns the value of attribute pos_x.
-
#pos_y ⇒ Object
Returns the value of attribute pos_y.
-
#px ⇒ Object
Returns the value of attribute px.
-
#stitch_map ⇒ Object
Returns the value of attribute stitch_map.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #black_and_white ⇒ Object
- #build_color_set(req_colors = []) ⇒ Object
- #build_off_color_arr(color) ⇒ Object
- #build_pixel_array ⇒ Object
- #colorize_pixels ⇒ Object
- #get_pixels ⇒ Object
-
#initialize(img_path = '', width = 50, px = 10, num_of_colors = 8, file_path = 'stitchify.svg') ⇒ Stitchifier
constructor
A new instance of Stitchifier.
- #make_img ⇒ Object
- #set_dominant_colors ⇒ Object
- #set_miro(cc, off_c) ⇒ Object
- #set_num_colors ⇒ Object
- #set_px_shapes(colors) ⇒ Object
- #stitch ⇒ Object
- #view_miro_opts ⇒ Object
Constructor Details
#initialize(img_path = '', width = 50, px = 10, num_of_colors = 8, file_path = 'stitchify.svg') ⇒ Stitchifier
Returns a new instance of Stitchifier.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/stitchify.rb', line 44 def initialize(img_path = '', width = 50, px=10, num_of_colors=8, file_path='stitchify.svg') # sets variables self.num_of_colors = num_of_colors set_num_colors self.width = width self.num_of_colors = num_of_colors self.img_path = img_path self.dominant_colors = [] self.stitch_map = [] self.px = px self.color_set = nil self.file_path = file_path unless img_path.empty? make_img build_color_set build_pixel_array stitch end end |
Instance Attribute Details
#base_pixel_arr ⇒ Object
Returns the value of attribute base_pixel_arr.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def base_pixel_arr @base_pixel_arr end |
#color_set ⇒ Object
Returns the value of attribute color_set.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def color_set @color_set end |
#dominant_colors ⇒ Object
Returns the value of attribute dominant_colors.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def dominant_colors @dominant_colors end |
#file_path ⇒ Object
Returns the value of attribute file_path.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def file_path @file_path end |
#img ⇒ Object
Returns the value of attribute img.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def img @img end |
#img_path ⇒ Object
Returns the value of attribute img_path.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def img_path @img_path end |
#num_of_colors ⇒ Object
Returns the value of attribute num_of_colors.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def num_of_colors @num_of_colors end |
#num_of_off_colors ⇒ Object
Returns the value of attribute num_of_off_colors.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def num_of_off_colors @num_of_off_colors end |
#pos_x ⇒ Object
Returns the value of attribute pos_x.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def pos_x @pos_x end |
#pos_y ⇒ Object
Returns the value of attribute pos_y.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def pos_y @pos_y end |
#px ⇒ Object
Returns the value of attribute px.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def px @px end |
#stitch_map ⇒ Object
Returns the value of attribute stitch_map.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def stitch_map @stitch_map end |
#width ⇒ Object
Returns the value of attribute width.
30 31 32 |
# File 'lib/stitchify.rb', line 30 def width @width end |
Instance Method Details
#black_and_white ⇒ Object
113 114 115 |
# File 'lib/stitchify.rb', line 113 def black_and_white [Pixelfy.new(0, 0, 0, 'x'), Pixelfy.new(0, 0, 100, 'circle')] end |
#build_color_set(req_colors = []) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/stitchify.rb', line 99 def build_color_set(req_colors = []) if req_colors.length == 0 set_dominant_colors self.color_set = self.dominant_colors else set_dominant_colors colors = self.dominant_colors colors.slice!(0, req_colors.length) color_map = colors + req_colors.map{ |x| Pixelfy.from_hex(x) } self.color_set = set_px_shapes(color_map) end build_pixel_array end |
#build_off_color_arr(color) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/stitchify.rb', line 164 def build_off_color_arr(color) palette = [] if !!color c = Chroma::Color.new(color) palette = c.palette case self.num_of_off_colors when 0 palette = [] when 1 palette = palette.complement.map{|x| Pixelfy.from_hex(x.to_s)} when 2 palette = palette.triad.map{|x| Pixelfy.from_hex(x.to_s)} when 3 palette = palette.tetrad.map{|x| Pixelfy.from_hex(x.to_s)} end end palette end |
#build_pixel_array ⇒ Object
117 118 119 120 |
# File 'lib/stitchify.rb', line 117 def build_pixel_array get_pixels colorize_pixels end |
#colorize_pixels ⇒ Object
134 135 136 137 |
# File 'lib/stitchify.rb', line 134 def colorize_pixels self.stitch_map = [] self.base_pixel_arr.each {|px| self.stitch_map << px.colorize(self.color_set) } end |
#get_pixels ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/stitchify.rb', line 122 def get_pixels px = [] unless self.img.nil? self.img.each_pixel do | pixel, col, row | pixel = pixel.to_hsla px << Pixelfy.new(pixel[0], pixel[1], pixel[2]) end end self.base_pixel_arr = px end |
#make_img ⇒ Object
71 72 73 |
# File 'lib/stitchify.rb', line 71 def make_img self.img = ImageList.new(img_path).resize_to_fit(width) end |
#set_dominant_colors ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/stitchify.rb', line 75 def set_dominant_colors colors = black_and_white set_num_colors if self.num_of_colors > 3 && !img_path.empty? miro_data = Miro::DominantColors.new(self.img_path).to_hex unless self.img_path.empty? main_color = miro_data.slice!(0, 1)[0] miro_px = miro_data.map{|x| Pixelfy.from_hex(x)} off_colors = build_off_color_arr(main_color) colors = miro_px + off_colors + colors end set_px_shapes(colors) self.dominant_colors = colors.uniq end |
#set_miro(cc, off_c) ⇒ Object
159 160 161 162 |
# File 'lib/stitchify.rb', line 159 def set_miro(cc, off_c) Miro.[:color_count] = cc self.num_of_off_colors = off_c end |
#set_num_colors ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/stitchify.rb', line 139 def set_num_colors num = self.num_of_colors - 2 case num when 1 set_miro(1, 0) when 2 set_miro(1, 1) when 3 set_miro(2, 1) when 4 set_miro(2, 2) when 5 set_miro(3, 2) when 6 set_miro(3, 3) else set_miro(num - 3, 3) end end |
#set_px_shapes(colors) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/stitchify.rb', line 89 def set_px_shapes(colors) color_pos = 0 colors.each do |px| if px.shape.nil? px.shape = FILLABLE_SHAPES[color_pos] color_pos = (color_pos + 1) % FILLABLE_SHAPES.length end end end |
#stitch ⇒ Object
66 67 68 69 |
# File 'lib/stitchify.rb', line 66 def stitch d = DrawRasem.new(self.stitch_map, self.width, self.px, self.color_set, self.file_path) d.stitch end |
#view_miro_opts ⇒ Object
183 184 185 |
# File 'lib/stitchify.rb', line 183 def view_miro_opts Miro. end |