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',
    '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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stitchify.rb', line 41

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

    unless img_path.empty?
        make_img
        set_dominant_colors
        build_pixel_array

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

Instance Attribute Details

#base_pixel_arrObject

Returns the value of attribute base_pixel_arr.



29
30
31
# File 'lib/stitchify.rb', line 29

def base_pixel_arr
  @base_pixel_arr
end

#dominant_colorsObject

Returns the value of attribute dominant_colors.



29
30
31
# File 'lib/stitchify.rb', line 29

def dominant_colors
  @dominant_colors
end

#imgObject

Returns the value of attribute img.



29
30
31
# File 'lib/stitchify.rb', line 29

def img
  @img
end

#img_pathObject

Returns the value of attribute img_path.



29
30
31
# File 'lib/stitchify.rb', line 29

def img_path
  @img_path
end

#num_of_colorsObject

Returns the value of attribute num_of_colors.



29
30
31
# File 'lib/stitchify.rb', line 29

def num_of_colors
  @num_of_colors
end

#num_of_off_colorsObject

Returns the value of attribute num_of_off_colors.



29
30
31
# File 'lib/stitchify.rb', line 29

def num_of_off_colors
  @num_of_off_colors
end

#pos_xObject

Returns the value of attribute pos_x.



29
30
31
# File 'lib/stitchify.rb', line 29

def pos_x
  @pos_x
end

#pos_yObject

Returns the value of attribute pos_y.



29
30
31
# File 'lib/stitchify.rb', line 29

def pos_y
  @pos_y
end

#pxObject

Returns the value of attribute px.



29
30
31
# File 'lib/stitchify.rb', line 29

def px
  @px
end

#stitch_mapObject

Returns the value of attribute stitch_map.



29
30
31
# File 'lib/stitchify.rb', line 29

def stitch_map
  @stitch_map
end

#widthObject

Returns the value of attribute width.



29
30
31
# File 'lib/stitchify.rb', line 29

def width
  @width
end

Instance Method Details

#black_and_whiteObject



86
87
88
# File 'lib/stitchify.rb', line 86

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

#build_off_color_arr(color) ⇒ Object



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

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



90
91
92
93
# File 'lib/stitchify.rb', line 90

def build_pixel_array
    get_pixels
    colorize_pixels
end

#colorize_pixelsObject



107
108
109
# File 'lib/stitchify.rb', line 107

def colorize_pixels
    self.base_pixel_arr.each {|px| self.stitch_map << px.colorize(self.dominant_colors) }
end

#get_pixelsObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/stitchify.rb', line 95

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



62
63
64
# File 'lib/stitchify.rb', line 62

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

#set_dominant_colorsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stitchify.rb', line 66

def set_dominant_colors
    color_pos = 0
    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
    colors.each do |px|
        if px.shape.nil?
            px.shape = FILLABLE_SHAPES[color_pos]
            color_pos = (color_pos + 1) % FILLABLE_SHAPES.length
        end
    end
    self.dominant_colors = colors.uniq
end

#set_miro(cc, off_c) ⇒ Object



131
132
133
134
# File 'lib/stitchify.rb', line 131

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

#set_num_colorsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stitchify.rb', line 111

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

#view_miro_optsObject



155
156
157
# File 'lib/stitchify.rb', line 155

def view_miro_opts
    Miro.options
end