Module: Functions::Operations::Effects

Extended by:
Effects
Included in:
Effects
Defined in:
lib/magick/functions/operations/effects.rb

Overview

Methods for performing various visual effects on a Compass::Magick::Canvas, e.g., fade, desaturate, etc.

Instance Method Summary collapse

Instance Method Details

#brightness(adjust = nil) ⇒ Effect

Adjusts the brightness of a color by changing its [R, G, B] components by a given value.

Copyright © 2010, Ryan LeFevre www.camanjs.com

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Brightness value as a float between -1.0 and 1.0.

Returns:

  • (Effect)

    A command which applies the Brightness to the canvas.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/magick/functions/operations/effects.rb', line 30

def brightness(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  brightness_adjust = 255 * Compass::Magick::Utils.value_of(adjust, 1.0, 0.5)
  Effect.new do |pixel|
    ChunkyPNG::Color.rgba(
      Effect.clamp(ChunkyPNG::Color.r(pixel) + brightness_adjust),
      Effect.clamp(ChunkyPNG::Color.g(pixel) + brightness_adjust),
      Effect.clamp(ChunkyPNG::Color.b(pixel) + brightness_adjust),
      ChunkyPNG::Color.a(pixel)
    )
  end
end

#contrast(adjust = nil) ⇒ Effect

Adjusts the contrast of a color by changing its [R, G, B] components by a given value.

Copyright © 2010, Ryan LeFevre www.camanjs.com

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Contrast value as a float, above 0.0.

Returns:

  • (Effect)

    A command which applies the contrast to the canvas.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/magick/functions/operations/effects.rb', line 52

def contrast(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  contrast_adjust = (1.0 + Compass::Magick::Utils.value_of(adjust, 1.0, 0.5))
  Effect.new do |pixel|
    ChunkyPNG::Color.rgba(
      Effect.clamp(((ChunkyPNG::Color.r(pixel) / 255.0 - 0.5) * contrast_adjust + 0.5) * 255),
      Effect.clamp(((ChunkyPNG::Color.g(pixel) / 255.0 - 0.5) * contrast_adjust + 0.5) * 255),
      Effect.clamp(((ChunkyPNG::Color.b(pixel) / 255.0 - 0.5) * contrast_adjust + 0.5) * 255),
      ChunkyPNG::Color.a(pixel)
    )
  end
end

#fade(adjust = nil) ⇒ Effect

Adjusts the intensity of a color by changing its alpha by a given value.

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Fade value as a float between 0.0 and 1.0.

Returns:

  • (Effect)

    A command which applies the fade to the canvas.



14
15
16
17
18
# File 'lib/magick/functions/operations/effects.rb', line 14

def fade(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  fade_adjust = 255 - (255 * Compass::Magick::Utils.value_of(adjust, 1.0, 0.5)).to_i
  Effect.new { |pixel| ChunkyPNG::Color.fade(pixel, fade_adjust) }
end

#grayscale(adjust = nil) ⇒ Effect Also known as: greyscale

Adjusts the intensity of a color by changing its [R, G, B] components according to BT709 luminosity factors.

Copyright © 2010, Ryan LeFevre www.camanjs.com

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Grayscale factor as a float, above 0.0 and below 1.0.

Returns:

  • (Effect)

    A command which applies the intensity to the canvas.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/magick/functions/operations/effects.rb', line 124

def grayscale(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  grayscale_adjust = [0, [Compass::Magick::Utils.value_of(adjust, 1.0, 0.5), 1.0].min].max
  Effect.new do |pixel|
    r    = ChunkyPNG::Color.r(pixel)
    g    = ChunkyPNG::Color.g(pixel)
    b    = ChunkyPNG::Color.b(pixel)
    gray = r * 0.2125 + g * 0.7154 + b * 0.0721
    r    = r + (gray - r) * grayscale_adjust unless r == gray
    g    = g + (gray - g) * grayscale_adjust unless g == gray
    b    = b + (gray - b) * grayscale_adjust unless b == gray
    ChunkyPNG::Color.rgba(Effect.clamp(r), Effect.clamp(g), Effect.clamp(b), ChunkyPNG::Color.a(pixel))
  end
end

#saturation(adjust = nil) ⇒ Effect

Adjusts the saturation of a color by changing its [R, G, B] components matching the highest intensity.

Copyright © 2010, Ryan LeFevre www.camanjs.com

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Saturation value as a float, above 0.0.

Returns:

  • (Effect)

    A command which applies the saturation to the canvas.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/magick/functions/operations/effects.rb', line 74

def saturation(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  saturation_adjust = Compass::Magick::Utils.value_of(adjust, 1.0, 0.5) * -1
  Effect.new do |pixel|
    r   = ChunkyPNG::Color.r(pixel)
    g   = ChunkyPNG::Color.g(pixel)
    b   = ChunkyPNG::Color.b(pixel)
    max = [r, g, b].max
    r   = r + (max - r) * saturation_adjust unless r == max
    g   = g + (max - g) * saturation_adjust unless g == max
    b   = b + (max - b) * saturation_adjust unless b == max
    ChunkyPNG::Color.rgba(Effect.clamp(r), Effect.clamp(g), Effect.clamp(b), ChunkyPNG::Color.a(pixel))
  end
end

#vibrance(adjust = nil) ⇒ Effect

Adjusts the vibrance of a color by changing its [R, G, B] components matching the average intensity.

Copyright © 2010, Ryan LeFevre www.camanjs.com

Parameters:

  • adjust (Sass::Script::Number) (defaults to: nil)

    Vibrance value as a float, above 0.0.

Returns:

  • (Effect)

    A command which applies the vibrance to the canvas.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/magick/functions/operations/effects.rb', line 98

def vibrance(adjust = nil)
  Compass::Magick::Utils.assert_type 'adjust', adjust, Sass::Script::Number
  vibrance_adjust = Compass::Magick::Utils.value_of(adjust, 1.0, 0.5) * -1
  Effect.new do |pixel|
    r       = ChunkyPNG::Color.r(pixel)
    g       = ChunkyPNG::Color.g(pixel)
    b       = ChunkyPNG::Color.b(pixel)
    max     = [r, g, b].max
    average = (r + g + b) / 3.0
    amount  = (((max - average).abs * 2.0) * vibrance_adjust) / 100.0
    r       = r + (max - r) * amount unless r == max
    g       = g + (max - g) * amount unless g == max
    b       = b + (max - b) * amount unless b == max
    ChunkyPNG::Color.rgba(Effect.clamp(r), Effect.clamp(g), Effect.clamp(b), ChunkyPNG::Color.a(pixel))
  end
end