Class: Gifenc::Image::Brush

Inherits:
Object
  • Object
show all
Defined in:
lib/image.rb

Overview

Represents a type of drawing brush, and encapsulates all the logic necessary to use it, such as the weight, shape, anchor point, color, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pixels, color = nil) ⇒ Brush

Create a new brush by providing the raw pixels that form it. For common shapes, you may instead prefer to use one of the helpers, such as square.

Parameters:

  • pixels (Array<Array<Integer>>)

    Relative coordinates of the pixels that compose the brush (see #pixels).

  • color (Integer) (defaults to: nil)

    Index of default brush color.



1187
1188
1189
1190
# File 'lib/image.rb', line 1187

def initialize(pixels, color = nil)
  @pixels = pixels
  @color = color
end

Instance Attribute Details

#colorInteger

The index in the color table of the default color to use when painting with this brush. It can be overidden whenever it's actually used.

Returns:

  • (Integer)

    Default color index.



1151
1152
1153
# File 'lib/image.rb', line 1151

def color
  @color
end

#pixelsArray<Array<Integer>>

Actual pixels that form the brush, and that will be drawn when using it. It is an array of pairs of coordinates, representing the X and Y offsets from the drawing point that will be painted. For example, if pixels = [[0, -1], [-1, 0], [0, 0], [1, 0], [0, 1]] then the brush will be a small cross centered at the drawing point.

Returns:

  • (Array<Array<Integer>>)

    Coordinates of the brush relative to the drawing point.



1146
1147
1148
# File 'lib/image.rb', line 1146

def pixels
  @pixels
end

Class Method Details

.square(weight = 1, color = nil, anchor = [0, 0]) ⇒ Brush

Creates a square brush of a given size.

Parameters:

  • weight (Float) (defaults to: 1)

    Size of the brush (side of the square) in pixels.

  • color (Integer) (defaults to: nil)

    Index of the color to use as default for drawing when no explicit color is provided.

  • anchor (Array<Float>) (defaults to: [0, 0])

    The anchor determines the position of the brush with respect to the drawing coordinates. It goes from -1, -1 to 1, 1. [0, 0] would mean the brush is centered.

Returns:

  • (Brush)

    The new square brush.



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/image.rb', line 1162

def self.square(weight = 1, color = nil, anchor = [0, 0])
  weight = weight.to_f
  weight = 1.0 if weight < 1.0
  shift_x = ((1 - anchor[0]) * (weight - 1) / 2).round
  shift_y = ((1 - anchor[1]) * (weight - 1) / 2).round
  weight = weight.round

  xlim_inf = -shift_x
  xlim_sup = xlim_inf + weight
  ylim_inf = -shift_y
  ylim_sup = ylim_inf + weight

  new(
    (xlim_inf ... xlim_sup).to_a.product((ylim_inf ... ylim_sup).to_a),
    color
  )
end

Instance Method Details

#draw(x, y, img, color = @color, bbox: nil, avoid: []) ⇒ Object

Use the brush to draw once on an image at the specified point. If no color is specified, the brush's default color will be used. A bounding box can be provided to restrict where in the image the drawing may happen. If it's not specified, the whole image will determine this box.

Parameters:

  • x (Integer)

    X coordinate of the drawing point.

  • y (Integer)

    Y coordinate of the drawing point.

  • img (Image)

    Image to draw onto.

  • color (Integer) (defaults to: @color)

    Index of the color in the color table to use.

  • bbox (Array<Integer>) (defaults to: nil)

    Bounding box determining the drawing region, in the format [X, Y, W, H].

  • avoid (Array<Integer>) (defaults to: [])

    List of colors over which the brush should NOT paint.

Raises:



1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/image.rb', line 1204

def draw(x, y, img, color = @color, bbox: nil, avoid: [])
  raise Exception::CanvasError, "No provided color nor default color found." if !color
  bbox = [0, 0, img.width, img.height] if !bbox
  @pixels.each{ |dx, dy|
    if Geometry.bound_check([[x + dx, y + dy]], bbox, true) && !avoid.include?(img[x + dx, y + dy])
      img[x + dx, y + dy] = color
    end
  }
end