Class: Gifenc::Image::Brush
- Inherits:
-
Object
- Object
- Gifenc::Image::Brush
- 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
-
#color ⇒ Integer
The index in the color table of the default color to use when painting with this brush.
-
#pixels ⇒ Array<Array<Integer>>
Actual pixels that form the brush, and that will be drawn when using it.
Class Method Summary collapse
-
.square(weight = 1, color = nil, anchor = [0, 0]) ⇒ Brush
Creates a square brush of a given size.
Instance Method Summary collapse
-
#draw(x, y, img, color = @color, bbox: nil, avoid: []) ⇒ Object
Use the brush to draw once on an image at the specified point.
-
#initialize(pixels, color = nil) ⇒ Brush
constructor
Create a new brush by providing the raw pixels that form it.
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.
1187 1188 1189 1190 |
# File 'lib/image.rb', line 1187 def initialize(pixels, color = nil) @pixels = pixels @color = color end |
Instance Attribute Details
#color ⇒ Integer
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.
1151 1152 1153 |
# File 'lib/image.rb', line 1151 def color @color end |
#pixels ⇒ Array<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.
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.
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.
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 |