Class: Smplkit::Platform::Color
- Inherits:
-
Object
- Object
- Smplkit::Platform::Color
- Defined in:
- lib/smplkit/platform/types.rb
Overview
Instance Attribute Summary collapse
-
#hex ⇒ Object
readonly
Returns the value of attribute hex.
Class Method Summary collapse
-
.rgb(red, green, blue) ⇒ Color
Construct a
Colorfrom 0-255 RGB components.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(hex) ⇒ Color
constructor
Build a
Colorfrom a CSS hex string. - #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(hex) ⇒ Color
Build a Color from a 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
#hex ⇒ Object (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.
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 |
#hash ⇒ Object
76 |
# File 'lib/smplkit/platform/types.rb', line 76 def hash = @hex.hash |
#to_s ⇒ Object
72 |
# File 'lib/smplkit/platform/types.rb', line 72 def to_s = @hex |
#to_str ⇒ Object
73 |
# File 'lib/smplkit/platform/types.rb', line 73 def to_str = @hex |