Class: Stylegen::Color
- Inherits:
-
Object
- Object
- Stylegen::Color
- Defined in:
- lib/stylegen/colors/color.rb
Constant Summary collapse
- SIX_DIGIT_HEX_REGEX =
/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/.freeze
- THREE_DIGIT_HEX_REGEX =
/^#?([a-f\d])([a-f\d])([a-f\d])$/.freeze
- MAX_PRECISION =
16
Instance Attribute Summary collapse
-
#alpha ⇒ Object
readonly
Returns the value of attribute alpha.
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Class Method Summary collapse
Instance Method Summary collapse
- #grayscale? ⇒ Boolean
-
#initialize(red, green, blue, alpha) ⇒ Color
constructor
A new instance of Color.
- #to_s(struct_name, indent = 0) ⇒ Object
Constructor Details
#initialize(red, green, blue, alpha) ⇒ Color
Returns a new instance of Color.
11 12 13 14 15 16 |
# File 'lib/stylegen/colors/color.rb', line 11 def initialize(red, green, blue, alpha) @red = red @green = green @blue = blue @alpha = alpha end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
5 6 7 |
# File 'lib/stylegen/colors/color.rb', line 5 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
5 6 7 |
# File 'lib/stylegen/colors/color.rb', line 5 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
5 6 7 |
# File 'lib/stylegen/colors/color.rb', line 5 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
5 6 7 |
# File 'lib/stylegen/colors/color.rb', line 5 def red @red end |
Class Method Details
.from_hex(hex, alpha = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/stylegen/colors/color.rb', line 18 def self.from_hex(hex, alpha = nil) if (match = hex.downcase.match(SIX_DIGIT_HEX_REGEX)) r = Integer(match.captures[0], 16) / 255.0 g = Integer(match.captures[1], 16) / 255.0 b = Integer(match.captures[2], 16) / 255.0 elsif (match = hex.downcase.match(THREE_DIGIT_HEX_REGEX)) r = Integer(match.captures[0] * 2, 16) / 255.0 g = Integer(match.captures[1] * 2, 16) / 255.0 b = Integer(match.captures[2] * 2, 16) / 255.0 else raise ArgumentError, "Invalid color syntax: #{hex}" end Color.new( r.round(MAX_PRECISION), g.round(MAX_PRECISION), b.round(MAX_PRECISION), alpha || 1.0 ) end |
Instance Method Details
#grayscale? ⇒ Boolean
39 40 41 |
# File 'lib/stylegen/colors/color.rb', line 39 def grayscale? @red == @green && @green == @blue end |
#to_s(struct_name, indent = 0) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/stylegen/colors/color.rb', line 43 def to_s(struct_name, indent = 0) if grayscale? "#{struct_name}(white: #{@red}, alpha: #{@alpha})" else indent_prefix = ' ' * indent result = [] result << "#{struct_name}(" result << "#{indent_prefix} red: #{@red}," result << "#{indent_prefix} green: #{@green}," result << "#{indent_prefix} blue: #{@blue}," result << "#{indent_prefix} alpha: #{@alpha}" result << "#{indent_prefix})" result.join("\n") end end |