Class: Shoes::Color

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shoes::Common::Inspect

#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
    message = <<~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, message
  end
  alpha ||= OPAQUE
  @red = normalize_rgb(red)
  @green = normalize_rgb(green)
  @blue = normalize_rgb(blue)
  @alpha = normalize_rgb(alpha)
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



38
39
40
# File 'shoes-core/lib/shoes/color.rb', line 38

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



38
39
40
# File 'shoes-core/lib/shoes/color.rb', line 38

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



38
39
40
# File 'shoes-core/lib/shoes/color.rb', line 38

def green
  @green
end

#redObject (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

.create(color) ⇒ Object



11
12
13
# File 'shoes-core/lib/shoes/color.rb', line 11

def self.create(color)
  color.is_a?(Color) ? color : new(color)
end

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

Returns:

  • (Boolean)


60
61
62
# File 'shoes-core/lib/shoes/color.rb', line 60

def black?
  @red.zero? && @green.zero? && @blue.zero?
end

#dark?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'shoes-core/lib/shoes/color.rb', line 44

def dark?
  @red + @green + @blue < 255 # 0x55 * 3
end

#hexString

Returns a hex represenation of this color.

Examples:

Shoes::Color.new(255, 0, 255).hex # => "#ff00ff"

Returns:

  • (String)

    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

Returns:

  • (Boolean)


40
41
42
# File 'shoes-core/lib/shoes/color.rb', line 40

def light?
  @red + @green + @blue > 510 # 0xaa * 3
end

#opaque?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'shoes-core/lib/shoes/color.rb', line 52

def opaque?
  @alpha == OPAQUE
end

#to_sObject



80
81
82
# File 'shoes-core/lib/shoes/color.rb', line 80

def to_s
  "rgb(#{red}, #{green}, #{blue})"
end

#transparent?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'shoes-core/lib/shoes/color.rb', line 48

def transparent?
  @alpha == TRANSPARENT
end

#white?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'shoes-core/lib/shoes/color.rb', line 56

def white?
  @red == 255 && @green == 255 && @blue == 255
end