Class: Smplkit::Platform::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/platform/types.rb

Overview

A color, expressed as a CSS hex string.

Smplkit::Color.new("#ef4444")        # 6-digit hex
Smplkit::Color.new("#fff")           # 3-digit shorthand
Smplkit::Color.new("#ef4444aa")      # 8-digit with alpha
Smplkit::Color.rgb(239, 68, 68)      # RGB components

Frozen — construct a fresh Color to change a value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex) ⇒ Color

Build a Color from a CSS hex string.

Parameters:

  • hex (String)

    A CSS hex string like “#RGB”, “#RRGGBB”, or “#RRGGBBAA”. Normalized to lowercase.

Raises:

  • (TypeError)

    If hex is not a String.

  • (ArgumentError)

    If hex is not a valid CSS hex string.



40
41
42
43
44
45
46
47
48
49
# File 'lib/smplkit/platform/types.rb', line 40

def initialize(hex)
  raise TypeError, "Color hex must be a String, got #{hex.class}: #{hex.inspect}" unless hex.is_a?(String)
  unless HEX_RE.match?(hex)
    raise ArgumentError,
          "Invalid color #{hex.inspect}: must be a CSS hex string like '#RGB', '#RRGGBB', or '#RRGGBBAA'"
  end

  @hex = hex.downcase.freeze
  freeze
end

Instance Attribute Details

#hexObject (readonly)

Returns the value of attribute hex.



32
33
34
# File 'lib/smplkit/platform/types.rb', line 32

def hex
  @hex
end

Class Method Details

.rgb(red, green, blue) ⇒ Color

Construct a Color from 0-255 RGB components.

Parameters:

  • red (Integer)

    Red component, an integer in the range 0-255.

  • green (Integer)

    Green component, an integer in the range 0-255.

  • blue (Integer)

    Blue component, an integer in the range 0-255.

Returns:

  • (Color)

    A color with the equivalent hex value.

Raises:

  • (TypeError)

    If any component is not an integer.

  • (ArgumentError)

    If any component is outside the range 0-255.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/smplkit/platform/types.rb', line 59

def self.rgb(red, green, blue)
  [%w[red green blue], [red, green, blue]].transpose.each do |name, val|
    unless val.is_a?(Integer) && !val.is_a?(TrueClass) && !val.is_a?(FalseClass)
      raise TypeError,
            "Color.rgb #{name} must be an Integer, got #{val.class}"
    end
    raise ArgumentError, "Color.rgb #{name} must be in range 0-255, got #{val.inspect}" unless val.between?(0,
                                                                                                            255)
  end

  new("##{red.to_s(16).rjust(2, "0")}#{green.to_s(16).rjust(2, "0")}#{blue.to_s(16).rjust(2, "0")}")
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



74
# File 'lib/smplkit/platform/types.rb', line 74

def ==(other) = other.is_a?(Color) && other.hex == @hex

#hashObject



76
# File 'lib/smplkit/platform/types.rb', line 76

def hash = @hex.hash

#to_sObject



72
# File 'lib/smplkit/platform/types.rb', line 72

def to_s = @hex

#to_strObject



73
# File 'lib/smplkit/platform/types.rb', line 73

def to_str = @hex