Class: Abachrome::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/abachrome/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color_space, coordinates, alpha = AbcDecimal("1.0")) ⇒ Color

Initializes a new Color object with the specified color space, coordinates, and alpha value.

Raises:

  • (ArgumentError)

    If the coordinates are invalid for the specified color space



36
37
38
39
40
41
42
# File 'lib/abachrome/color.rb', line 36

def initialize(color_space, coordinates, alpha = AbcDecimal("1.0"))
  @color_space = color_space
  @coordinates = coordinates.map { |c| AbcDecimal(c.to_s) }
  @alpha = AbcDecimal.new(alpha.to_s)

  validate_coordinates!
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



27
28
29
# File 'lib/abachrome/color.rb', line 27

def alpha
  @alpha
end

#color_spaceObject (readonly)

Returns the value of attribute color_space.



27
28
29
# File 'lib/abachrome/color.rb', line 27

def color_space
  @color_space
end

#coordinatesObject (readonly)

Returns the value of attribute coordinates.



27
28
29
# File 'lib/abachrome/color.rb', line 27

def coordinates
  @coordinates
end

Class Method Details

.from_lrgb(r, g, b, a = 1.0) ⇒ Abachrome::Color

Creates a new Color instance from LRGB values



72
73
74
75
# File 'lib/abachrome/color.rb', line 72

def self.from_lrgb(r, g, b, a = 1.0)
  space = ColorSpace.find(:lrgb)
  new(space, [r, g, b], a)
end

.from_oklab(l, a, b, alpha = 1.0) ⇒ Abachrome::Color

Creates a new Color object with OKLAB values.



84
85
86
87
# File 'lib/abachrome/color.rb', line 84

def self.from_oklab(l, a, b, alpha = 1.0)
  space = ColorSpace.find(:oklab)
  new(space, [l, a, b], alpha)
end

.from_oklch(l, c, h, alpha = 1.0) ⇒ Abachrome::Color

Creates a new color instance in the OKLCH color space.



96
97
98
99
# File 'lib/abachrome/color.rb', line 96

def self.from_oklch(l, c, h, alpha = 1.0)
  space = ColorSpace.find(:oklch)
  new(space, [l, c, h], alpha)
end

.from_rgb(r, g, b, a = 1.0) ⇒ Abachrome::Color

Creates a new Color instance from RGB values



60
61
62
63
# File 'lib/abachrome/color.rb', line 60

def self.from_rgb(r, g, b, a = 1.0)
  space = ColorSpace.find(:srgb)
  new(space, [r, g, b], a)
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this color instance with another for equality.

Two colors are considered equal if they have the same color space, coordinates, and alpha value.



108
109
110
111
112
113
114
# File 'lib/abachrome/color.rb', line 108

def ==(other)
  return false unless other.is_a?(Color)

  color_space == other.color_space &&
    coordinates == other.coordinates &&
    alpha == other.alpha
end

#eql?(other) ⇒ Boolean

Checks if this color is equal to another color object.

See Also:



121
122
123
# File 'lib/abachrome/color.rb', line 121

def eql?(other)
  self == other
end

#hashInteger

Generates a hash code for this color instance based on its color space, coordinates, and alpha value. The method first converts these components to strings, then computes a hash of the resulting array.

and as a hash key in Hash objects



132
133
134
# File 'lib/abachrome/color.rb', line 132

def hash
  [color_space, coordinates, alpha].map(&:to_s).hash
end

#to_hexString

Returns the color as a hexadecimal string representation.



150
151
152
153
# File 'lib/abachrome/color.rb', line 150

def to_hex
  require_relative "outputs/css"
  Outputs::CSS.format_hex(self)
end

#to_sString

Returns a string representation of the color in the format “ColorSpaceName(coord1, coord2, coord3, alpha)”

color space name, coordinate values rounded to 3 decimal places, and alpha value (if not 1.0)



141
142
143
144
145
# File 'lib/abachrome/color.rb', line 141

def to_s
  coord_str = coordinates.map { |c| c.to_f.round(3) }.join(", ")
  alpha_str = alpha == AbcDecimal.new("1.0") ? "" : ", #{alpha.to_f.round(3)}"
  "#{color_space.name}(#{coord_str}#{alpha_str})"
end