Module: ChunkyPNG::Canvas::Operations

Included in:
ChunkyPNG::Canvas
Defined in:
lib/chunky_png/canvas/operations.rb

Instance Method Summary collapse

Instance Method Details

#change_mask_color!(new_color) ⇒ Object



55
56
57
58
# File 'lib/chunky_png/canvas/operations.rb', line 55

def change_mask_color!(new_color)
  raise ChunkyPNG::ExpectationFailed, "This is not a mask image!" if palette.opaque_palette.size != 1
  pixels.map! { |pixel| (new_color & 0xffffff00) | ChunkyPNG::Color.a(pixel) }
end

#change_theme_color!(old_theme_color, new_theme_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5) ⇒ Object



32
33
34
35
36
# File 'lib/chunky_png/canvas/operations.rb', line 32

def change_theme_color!(old_theme_color, new_theme_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)
  base, mask = extract_mask(old_theme_color, bg_color, tolerance)
  mask.change_mask_color!(new_theme_color)
  self.replace(base.compose(mask))
end

#compose(new_foreground, dx = 0, dy = 0) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/chunky_png/canvas/operations.rb', line 4

def compose(new_foreground, dx = 0, dy = 0)
  check_size_constraints!(new_foreground, dx, dy)

  for y in 0...new_foreground.height do
    for x in 0...new_foreground.width do
      self[x+dx, y+dy] = ChunkyPNG::Color.compose(new_foreground[x, y], self[x+dx, y+dy])
    end
  end
  self
end

#crop(x, y, crop_width, crop_height) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/chunky_png/canvas/operations.rb', line 24

def crop(x, y, crop_width, crop_height)
  new_pixels = []
  for cy in 0...crop_height do
    new_pixels += pixels.slice((cy + y) * width + x, crop_width)
  end
  ChunkyPNG::Canvas.new(crop_width, crop_height, new_pixels)
end

#extract_mask(mask_color, bg_color, tolerance = 5) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chunky_png/canvas/operations.rb', line 38

def extract_mask(mask_color, bg_color, tolerance = 5)
  base_pixels = []
  mask_pixels = []

  pixels.each do |pixel|
    if ChunkyPNG::Color.alpha_decomposable?(pixel, mask_color, bg_color, tolerance)
      mask_pixels << ChunkyPNG::Color.decompose_color(pixel, mask_color, bg_color, tolerance)
      base_pixels << bg_color
    else
      mask_pixels << (mask_color & 0xffffff00)
      base_pixels << pixel
    end
  end
  
  [ self.class.new(width, height, base_pixels), self.class.new(width, height, mask_pixels) ]
end

#replace(other, offset_x = 0, offset_y = 0) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/chunky_png/canvas/operations.rb', line 15

def replace(other, offset_x = 0, offset_y = 0)
  check_size_constraints!(other, offset_x, offset_y)

  for y in 0...other.height do
    pixels[(y + offset_y) * width + offset_x, other.width] = other.pixels[y * other.width, other.width]
  end
  self
end