Class: Stylegen::Color
- Inherits:
-
Object
- Object
- Stylegen::Color
- Defined in:
- lib/stylegen/colors/color.rb
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(r, g, b, a) ⇒ Color
constructor
A new instance of Color.
- #to_s(struct_name, indent = 0) ⇒ Object
Constructor Details
#initialize(r, g, b, a) ⇒ Color
Returns a new instance of Color.
7 8 9 |
# File 'lib/stylegen/colors/color.rb', line 7 def initialize(r, g, b, a) @red, @green, @blue, @alpha = r, g, b, a 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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/stylegen/colors/color.rb', line 11 def self.from_hex(hex, alpha = nil) if (match = hex.downcase.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/)) 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(/^#?([a-f\d])([a-f\d])([a-f\d])$/)) 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 max_precision = 16 Color.new(r.round(max_precision), g.round(max_precision), b.round(max_precision), alpha || 1.0) end |
Instance Method Details
#grayscale? ⇒ Boolean
29 30 31 |
# File 'lib/stylegen/colors/color.rb', line 29 def grayscale? @red == @green && @green == @blue end |
#to_s(struct_name, indent = 0) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stylegen/colors/color.rb', line 33 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 |