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
-
#brighten(amount = 10) ⇒ Color
Brightens the color by the given
amount
. -
#darken(amount = 10) ⇒ Color
Darkens the color by the given
amount
. -
#desaturate(amount = 10) ⇒ Color
Desaturates the color by the given
amount
. -
#grayscale ⇒ Color
(also: #greyscale)
Converts the color to grayscale.
-
#lighten(amount = 10) ⇒ Color
Lightens the color by the given
amount
. -
#opacity(amount) ⇒ Color
Sets color opacity to the given 'amount'.
-
#saturate(amount = 10) ⇒ Color
Saturates the color by the given
amount
. -
#spin(amount) ⇒ Color
Spins around the hue color wheel by
amount
in degrees.
Instance Method Details
#brighten(amount = 10) ⇒ Color
Brightens the color by the given amount
.
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
.
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
.
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 |
#grayscale ⇒ Color Also known as: greyscale
Converts the color to grayscale.
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
.
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'.
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
.
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.
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 |