Class: Shoes::Color
- Inherits:
-
Object
- Object
- Shoes::Color
- Includes:
- Comparable, Shoes::Common::Inspect
- Defined in:
- shoes-core/lib/shoes/color.rb,
shoes-core/lib/shoes/color/dsl_helpers.rb,
shoes-core/lib/shoes/color/hex_converter.rb
Defined Under Namespace
Modules: DSLHelpers Classes: HexConverter
Constant Summary collapse
- OPAQUE =
255
- TRANSPARENT =
0
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
- #<=>(other) ⇒ Object
- #black? ⇒ Boolean
- #dark? ⇒ Boolean
-
#hex ⇒ String
A hex represenation of this color.
-
#initialize(*args) ⇒ Color
constructor
red, green, blue, alpha = OPAQUE).
- #light? ⇒ Boolean
- #opaque? ⇒ Boolean
- #to_s ⇒ Object
- #transparent? ⇒ Boolean
- #white? ⇒ Boolean
Methods included from Shoes::Common::Inspect
Constructor Details
#initialize(*args) ⇒ Color
red, green, blue, alpha = OPAQUE)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'shoes-core/lib/shoes/color.rb', line 15 def initialize(*args) # red, green, blue, alpha = OPAQUE) case args.length when 1 red, green, blue, alpha = HexConverter.new(args.first).to_rgb when 3, 4 red, green, blue, alpha = *args else = <<~EOS Wrong number of arguments (#{args.length} for 1, 3, or 4). Must be one of: - #{self.class}.new(hex_string) - #{self.class}.new(red, green, blue) - #{self.class}.new(red, green, blue, alpha) EOS raise ArgumentError, end alpha ||= OPAQUE @red = normalize_rgb(red) @green = normalize_rgb(green) @blue = normalize_rgb(blue) @alpha = normalize_rgb(alpha) end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
38 39 40 |
# File 'shoes-core/lib/shoes/color.rb', line 38 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
38 39 40 |
# File 'shoes-core/lib/shoes/color.rb', line 38 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
38 39 40 |
# File 'shoes-core/lib/shoes/color.rb', line 38 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
38 39 40 |
# File 'shoes-core/lib/shoes/color.rb', line 38 def red @red end |
Class Method Details
Instance Method Details
#<=>(other) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'shoes-core/lib/shoes/color.rb', line 64 def <=>(other) return nil unless other.is_a?(self.class) if same_base_color?(other) @alpha <=> other.alpha else less_or_greater_than other end end |
#black? ⇒ Boolean
60 61 62 |
# File 'shoes-core/lib/shoes/color.rb', line 60 def black? @red.zero? && @green.zero? && @blue.zero? end |
#dark? ⇒ Boolean
44 45 46 |
# File 'shoes-core/lib/shoes/color.rb', line 44 def dark? @red + @green + @blue < 255 # 0x55 * 3 end |
#hex ⇒ String
Returns a hex represenation of this color.
76 77 78 |
# File 'shoes-core/lib/shoes/color.rb', line 76 def hex format "#%02x%02x%02x", @red, @green, @blue end |
#light? ⇒ Boolean
40 41 42 |
# File 'shoes-core/lib/shoes/color.rb', line 40 def light? @red + @green + @blue > 510 # 0xaa * 3 end |
#opaque? ⇒ Boolean
52 53 54 |
# File 'shoes-core/lib/shoes/color.rb', line 52 def opaque? @alpha == OPAQUE end |
#to_s ⇒ Object
80 81 82 |
# File 'shoes-core/lib/shoes/color.rb', line 80 def to_s "rgb(#{red}, #{green}, #{blue})" end |
#transparent? ⇒ Boolean
48 49 50 |
# File 'shoes-core/lib/shoes/color.rb', line 48 def transparent? @alpha == TRANSPARENT end |
#white? ⇒ Boolean
56 57 58 |
# File 'shoes-core/lib/shoes/color.rb', line 56 def white? @red == 255 && @green == 255 && @blue == 255 end |