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

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

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

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

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

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

#saturate(amount = 10) ⇒ Color

Saturates the color by the given amount.

Examples:

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

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

102
103
104
105
106
107
# File 'lib/chroma/color/modifiers.rb', line 102

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