Method: Pixelart::Image#sketch

Defined in:
lib/pixelart/sketch.rb

#sketch(sketch = 4, line: 1, line_color: Color::BLACK, colorize: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pixelart/sketch.rb', line 6

def sketch( sketch=4, line: 1,
                      line_color: Color::BLACK,
                      colorize: false )
    ## todo/check: rename color option to fill or such - why? why not?

     # todo: line - find a better name eg. line_strenght/width or such?
    width  = @img.width*sketch  + (@img.width+1)*line
    height = @img.height*sketch + (@img.height+1)*line

    puts "  #{width}x#{height}"


    background_color = colorize ? Color::TRANSPARENT
                                : Color::WHITE


    img = Image.new( width, height, background_color )


    @img.width.times do |x|
      @img.height.times do |y|
        pixel = @img[x,y]

        ## get surrounding pixels - if "out-of-bound" use transparent (0)
        left   =  x == 0  ? Color::TRANSPARENT : @img[x-1,y]
        top    =  y == 0  ? Color::TRANSPARENT : @img[x  ,y-1]
        diag   =  (x==0 || y== 0) ? Color::TRANSPARENT : @img[x-1,y-1]

        if pixel != left   ## draw vertical line
          line.times do |n|
            (sketch+line*2).times do |m|
                img[    x*sketch + line*x + n,
                    m + y*sketch + line*y] = line_color
              end
            end
        end

        if pixel != top   ## draw horizontal line
           (sketch+line*2).times do |n|
             line.times do |m|
               img[n + x*sketch + line*x,
                       y*sketch + line*y + m] = line_color
              end
           end
        end


        ## check special edge case for x and y to add "finish-up" right and bottom line
        if x == @img.width-1 && pixel != Color::TRANSPARENT
           ## draw vertical line
           line.times do |n|
            (sketch+line*2).times do |m|
              img[    (x+1)*sketch + line*(x+1) + n,
                  m + y*sketch + line*y] = line_color
            end
           end
        end

        if y== @img.height-1 && pixel != Color::TRANSPARENT
          ## draw horizontal line
          (sketch+line*2).times do |n|
            line.times do |m|
              img[n + x*sketch + line*x,
                      (y+1)*sketch + line*(y+1) + m] = line_color
            end
          end
        end

        ###############
        ## fill with pixel color if color true (default is false)
        if colorize && pixel != Color::TRANSPARENT
          sketch.times do |n|
            sketch.times do |m|
              img[x*sketch + line*(x+1) + n,
                  y*sketch + line*(y+1) + m] = pixel
            end
          end

          if pixel == left   ## draw vertical line
            line.times do |n|
              sketch.times do |m|
                img[x*sketch + line*x + n,
                    y*sketch + line*(y+1) + m]  = pixel
                    # (y%2==0 ? 0x0000ffff : 0x000088ff )  # (for debugging)
              end
            end
          end

          if pixel == top   ## draw horizontal line
            sketch.times do |n|
              line.times do |m|
                img[x*sketch + line*(x+1) + n,
                    y*sketch + line*y + m]   = pixel
                    # (x%2==0 ? 0xff0000ff : 0x880000ff ) # (for debugging)
               end
            end
         end

         ## check all four same color (00,01)
         ##                           (10, x)  - bingo!
         if pixel == left && pixel == top && pixel == diag
            line.times do |n|
              line.times do |m|
                img[x*sketch + line*x + n,
                    y*sketch + line*y + m]  = pixel
                # 0xffff00ff  # (for debugging)
               end
            end
         end
        end  # colorize?

      end #  height.times
    end   #  width.times

    img
end