Class: Stitchifier

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(img_path = '', width = 50, px = 10, num_of_colors = 8) ⇒ 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
# File 'lib/stitchify.rb', line 44

def initialize(img_path = '', width = 50, px=10, num_of_colors = 8)
    # 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

    unless img_path.empty?
        make_img
        build_color_set
        build_pixel_array

        stitch
    end
end

Instance Attribute Details

#base_pixel_arrObject

Returns the value of attribute base_pixel_arr.



31
32
33
# File 'lib/stitchify.rb', line 31

def base_pixel_arr
  @base_pixel_arr
end

#color_setObject

Returns the value of attribute color_set.



31
32
33
# File 'lib/stitchify.rb', line 31

def color_set
  @color_set
end

#dominant_colorsObject

Returns the value of attribute dominant_colors.



31
32
33
# File 'lib/stitchify.rb', line 31

def dominant_colors
  @dominant_colors
end

#imgObject

Returns the value of attribute img.



31
32
33
# File 'lib/stitchify.rb', line 31

def img
  @img
end

#img_pathObject

Returns the value of attribute img_path.



31
32
33
# File 'lib/stitchify.rb', line 31

def img_path
  @img_path
end

#num_of_colorsObject

Returns the value of attribute num_of_colors.



31
32
33
# File 'lib/stitchify.rb', line 31

def num_of_colors
  @num_of_colors
end

#num_of_off_colorsObject

Returns the value of attribute num_of_off_colors.



31
32
33
# File 'lib/stitchify.rb', line 31

def num_of_off_colors
  @num_of_off_colors
end

#pos_xObject

Returns the value of attribute pos_x.



31
32
33
# File 'lib/stitchify.rb', line 31

def pos_x
  @pos_x
end

#pos_yObject

Returns the value of attribute pos_y.



31
32
33
# File 'lib/stitchify.rb', line 31

def pos_y
  @pos_y
end

#pxObject

Returns the value of attribute px.



31
32
33
# File 'lib/stitchify.rb', line 31

def px
  @px
end

#stitch_mapObject

Returns the value of attribute stitch_map.



31
32
33
# File 'lib/stitchify.rb', line 31

def stitch_map
  @stitch_map
end

#widthObject

Returns the value of attribute width.



31
32
33
# File 'lib/stitchify.rb', line 31

def width
  @width
end

Instance Method Details

#black_and_whiteObject



112
113
114
# File 'lib/stitchify.rb', line 112

def black_and_white
    [Pixelfy.new(0, 0, 0, 'circle'), Pixelfy.new(0, 0, 100, 'x')]
end

#build_color_set(req_colors = []) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stitchify.rb', line 98

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/stitchify.rb', line 163

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_arrayObject



116
117
118
119
# File 'lib/stitchify.rb', line 116

def build_pixel_array
    get_pixels
    colorize_pixels
end

#colorize_pixelsObject



133
134
135
136
# File 'lib/stitchify.rb', line 133

def colorize_pixels
    self.stitch_map = []
    self.base_pixel_arr.each {|px| self.stitch_map << px.colorize(self.color_set) }
end

#get_pixelsObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/stitchify.rb', line 121

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_imgObject



70
71
72
# File 'lib/stitchify.rb', line 70

def make_img
    self.img = ImageList.new(img_path).resize_to_fit(width)
end

#set_dominant_colorsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stitchify.rb', line 74

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



158
159
160
161
# File 'lib/stitchify.rb', line 158

def set_miro(cc, off_c)
    Miro.options[:color_count] = cc
    self.num_of_off_colors = off_c
end

#set_num_colorsObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/stitchify.rb', line 138

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



88
89
90
91
92
93
94
95
96
# File 'lib/stitchify.rb', line 88

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

#stitchObject



65
66
67
68
# File 'lib/stitchify.rb', line 65

def stitch
    d = DrawRasem.new(self.stitch_map, self.width, self.px, self.color_set)
    d.stitch
end

#view_miro_optsObject



182
183
184
# File 'lib/stitchify.rb', line 182

def view_miro_opts
    Miro.options
end