Module: Abachrome::ColorMixins::Lighten

Defined in:
lib/abachrome/color_mixins/lighten.rb

Instance Method Summary collapse

Instance Method Details

#darken(amount = 0.1) ⇒ Color

Darkens a color by decreasing its lightness value.

This method is effectively a convenience wrapper around the #lighten method, passing a negative amount value to decrease the lightness instead of increasing it.

Defaults to 0.1 (10% darker).

Parameters:

  • amount (Float) (defaults to: 0.1)

    The amount to darken the color by, between 0 and 1.

Returns:

  • (Color)

    A new color instance with decreased lightness.

See Also:



72
73
74
# File 'lib/abachrome/color_mixins/lighten.rb', line 72

def darken(amount = 0.1)
  lighten(-amount)
end

#darken!(amount = 0.1) ⇒ Abachrome::Color

Decreases the lightness value of the color by the specified amount. Modifies the color in place.

Defaults to 0.1 (10% darker).

Parameters:

  • amount (Float) (defaults to: 0.1)

    The amount to darken the color by, as a value between 0 and 1.

Returns:

See Also:



83
84
85
# File 'lib/abachrome/color_mixins/lighten.rb', line 83

def darken!(amount = 0.1)
  lighten!(-amount)
end

#lighten(amount = 0.1) ⇒ Abachrome::Color

Increases the lightness of a color by the specified amount in the OKLab color space. This method works by extracting the L (lightness) component from the OKLab representation of the color and increasing it by the given amount, ensuring the result stays within the valid range of [0, 1].

value between 0 and 1. Defaults to 0.1 (10% increase).

Parameters:

  • amount (Numeric) (defaults to: 0.1)

    The amount to increase the lightness by, as a decimal

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/abachrome/color_mixins/lighten.rb', line 32

def lighten(amount = 0.1)
  amount = AbcDecimal(amount)
  oklab = to_oklab
  l, a, b = oklab.coordinates

  new_l = l + amount
  new_l = AbcDecimal("1.0") if new_l > 1
  new_l = AbcDecimal("0.0") if new_l.negative?

  Color.new(
    ColorSpace.find(:oklab),
    [new_l, a, b],
    alpha
  )
end

#lighten!(amount = 0.1) ⇒ Abachrome::Color

Increases the lightness of the color by the specified amount and modifies the current color object. This method changes the color in-place, mutating the current object. The color is converted to a lightness-based color space if needed to perform the operation.

between 0 and 1. Default is 0.1 (10% increase).

Parameters:

  • amount (Float) (defaults to: 0.1)

    The amount to increase the lightness by, as a decimal value

Returns:



55
56
57
58
59
60
61
# File 'lib/abachrome/color_mixins/lighten.rb', line 55

def lighten!(amount = 0.1)
  lightened = lighten(amount)
  @color_space = lightened.color_space
  @coordinates = lightened.coordinates
  @alpha = lightened.alpha
  self
end