Class: PSD::LayerStyles::ColorOverlay

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/renderer/layer_styles/color_overlay.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(styles) ⇒ ColorOverlay

Returns a new instance of ColorOverlay.



26
27
28
29
30
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 26

def initialize(styles)
  @canvas = styles.canvas
  @node = styles.node
  @data = styles.data
end

Class Method Details

.can_apply?(canvas, data) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 11

def self.can_apply?(canvas, data)
  data.has_key?('SoFi') && 
  data['SoFi']['enab'] &&
  canvas.node.header.rgb?
end

.for_canvas(canvas) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 17

def self.for_canvas(canvas)
  data = canvas.node.object_effects
  return nil if data.nil?
  return nil unless can_apply?(canvas, data.data)

  styles = LayerStyles.new(canvas)
  self.new(styles)
end

.should_apply?(canvas, data) ⇒ Boolean

TODO: CMYK support

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 5

def self.should_apply?(canvas, data)
  data.has_key?('SoFi') && 
  data['SoFi']['enab'] &&
  canvas.node.header.rgb?
end

Instance Method Details

#aObject Also known as: overlay_opacity



63
64
65
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 63

def a
  @a ||= (overlay_data['Opct'][:value] * 2.55).ceil
end

#apply!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 32

def apply!
  PSD.logger.debug "Layer style: layer = #{@node.name}, type = color overlay, blend mode = #{blending_mode}"

  @canvas.height.times do |y|
    @canvas.width.times do |x|
      pixel = @canvas[x, y]
      alpha = ChunkyPNG::Color.a(pixel)
      next if alpha == 0

      new_pixel = Compose.send(blending_mode, overlay_color, pixel, overlay_opacity)
      @canvas[x, y] = (new_pixel & 0xFFFFFF00) | alpha
    end
  end
end

#bObject



59
60
61
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 59

def b
  @b ||= color_data['Bl  '].round
end

#gObject



55
56
57
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 55

def g
  @g ||= color_data['Grn '].round
end

#overlay_colorObject



47
48
49
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 47

def overlay_color
  @overlay_color ||= ChunkyPNG::Color.rgb(r, g, b)
end

#rObject



51
52
53
# File 'lib/psd/renderer/layer_styles/color_overlay.rb', line 51

def r
  @r ||= color_data['Rd  '].round
end