Module: Chroma::Color::Modifiers

Included in:
Chroma::Color
Defined in:
lib/chroma/color/modifiers.rb

Overview

Methods that return a new modified Chroma::Color.

Instance Method Summary collapse

Instance Method Details

#brighten(amount = 10) ⇒ Color

Brightens the color by the given amount.

Examples:

'red'.paint.brighten     #=> #ff1a1a
'red'.paint.brighten(20) #=> #ff3333

Parameters:

  • amount (Fixnum) (defaults to: 10)

Returns:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chroma/color/modifiers.rb', line 27

def brighten(amount = 10)
  # Don't include alpha
  rgb = @rgb.to_a[0..2]
  amount = (255 * (-amount / 100.0)).round

  rgb.map! do |n|
    [0, [255, n - amount].min].max
  end

  self.class.new(ColorModes::Rgb.new(*rgb), @format)
end

#darken(amount = 10) ⇒ Color

Darkens the color by the given amount.

Examples:

'red'.paint.darken     #=> #cc0000
'red'.paint.darken(20) #=> #990000

Parameters:

  • amount (Fixnum) (defaults to: 10)

Returns:



47
48
49
50
51
# File 'lib/chroma/color/modifiers.rb', line 47

def darken(amount = 10)
  hsl = self.hsl
  hsl.l = clamp01(hsl.l - amount / 100.0)
  self.class.new(hsl, @format)
end

#desaturate(amount = 10) ⇒ Color

Desaturates the color by the given amount.

Examples:

'red'.paint.desaturate     #=> #f20d0d
'red'.paint.desaturate(20) #=> #e61919

Parameters:

  • amount (Fixnum) (defaults to: 10)

Returns:



61
62
63
64
65
# File 'lib/chroma/color/modifiers.rb', line 61

def desaturate(amount = 10)
  hsl = self.hsl
  hsl.s = clamp01(hsl.s - amount / 100.0)
  self.class.new(hsl, @format)
end

#grayscaleColor Also known as: greyscale

Converts the color to grayscale.

Examples:

'green'.paint.grayscale #=> #404040

Returns:



87
88
89
# File 'lib/chroma/color/modifiers.rb', line 87

def grayscale
  desaturate(100)
end

#lighten(amount = 10) ⇒ Color

Lightens the color by the given amount.

Examples:

'red'.paint.lighten     #=> #ff3333
'red'.paint.lighten(20) #=> #ff6666

Parameters:

  • amount (Fixnum) (defaults to: 10)

Returns:



13
14
15
16
17
# File 'lib/chroma/color/modifiers.rb', line 13

def lighten(amount = 10)
  hsl = self.hsl
  hsl.l = clamp01(hsl.l + amount / 100.0)
  self.class.new(hsl, @format)
end

#opacity(amount) ⇒ Color

Sets color opacity to the given 'amount'.

Examples:

'red'.paint.opacity(0.5)        #=> #ff0000
'red'.paint.opacity(0.5).to_rgb #=> 'rgba(255, 0, 0, 0.5)'

Parameters:

  • amount (Fixnum)

Returns:



101
102
103
104
# File 'lib/chroma/color/modifiers.rb', line 101

def opacity(amount)
  rgb = @rgb.to_a[0..2] + [amount]
  self.class.new(ColorModes::Rgb.new(*rgb), @format)
end

#saturate(amount = 10) ⇒ Color

Saturates the color by the given amount.

Examples:

'#123'.paint.saturate     #=> #0e2236
'#123'.paint.saturate(20) #=> #0a223a

Parameters:

  • amount (Fixnum) (defaults to: 10)

Returns:



75
76
77
78
79
# File 'lib/chroma/color/modifiers.rb', line 75

def saturate(amount = 10)
  hsl = self.hsl
  hsl.s = clamp01(hsl.s + amount / 100.0)
  self.class.new(hsl, @format)
end

#spin(amount) ⇒ Color

Spins around the hue color wheel by amount in degrees.

Examples:

'red'.paint.spin(30) #=> #ff8000
'red'.paint.spin(60) #=> yellow
'red'.paint.spin(90) #=> #80ff00

Parameters:

  • amount (Fixnum)

Returns:



115
116
117
118
119
120
# File 'lib/chroma/color/modifiers.rb', line 115

def spin(amount)
  hsl = self.hsl
  hue = (hsl.h.round + amount) % 360
  hsl.h = hue < 0 ? 360 + hue : hue
  self.class.new(hsl, @format)
end