Class: Inker::Color
- Inherits:
-
Object
- Object
- Inker::Color
- Extended by:
- Tools
- Includes:
- Serializers
- Defined in:
- lib/inker/color.rb,
lib/inker/color/tools.rb,
lib/inker/color/serializers.rb
Overview
This class is used to represent a color in Ruby as an object. It allows to create a new instance of Color from a string which represents a color. It also allows to obtain more info about the color and convert color to a different format.
Defined Under Namespace
Modules: Serializers, Tools
Constant Summary
Constants included from Tools
Tools::HEX_REGEX, Tools::HSLA_REGEX, Tools::HSL_REGEX, Tools::RGBA_REGEX, Tools::RGB_REGEX
Instance Attribute Summary collapse
-
#alpha ⇒ Object
Returns the value of attribute alpha.
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#red ⇒ Object
Returns the value of attribute red.
Instance Method Summary collapse
- #==(color) ⇒ Object
-
#brightness ⇒ Integer
Calculate the brightness of a color.
-
#dark? ⇒ Boolean
Returns a boolean which indicates if the color is dark.
-
#hue ⇒ Integer
Calculate the HUE of a color.
-
#initialize(color_str) ⇒ Color
constructor
Create a new Color object from a color string.
-
#light? ⇒ Boolean
Returns a boolean which indicates if the color is light.
-
#lightness ⇒ Float
Calculate the lightness of a color.
-
#saturation ⇒ Float
Calculate the saturation of a color.
-
#to_s(format = 'hex') ⇒ String
Convert color to string in the specified format.
Methods included from Tools
from_custom_string, from_hsl, from_hsla, from_rgb, from_rgba, hsl_to_rgb, is_hex?, is_hsl?, is_hsla?, is_rgb?, is_rgba?, parse_color, random
Methods included from Serializers
#hex, #hex6, #hsl, #hsla, #rgb, #rgba
Constructor Details
#initialize(color_str) ⇒ Color
Create a new Inker::Color object from a color string.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/inker/color.rb', line 18 def initialize(color_str) @input = color_str.to_s.downcase.gsub(/\s+/, "") Color.parse_color(@input).tap do |color| @red = color[:red] @green = color[:green] @blue = color[:blue] @alpha = color[:alpha] end validate_color! end |
Instance Attribute Details
#alpha ⇒ Object
Returns the value of attribute alpha.
13 14 15 |
# File 'lib/inker/color.rb', line 13 def alpha @alpha end |
#blue ⇒ Object
Returns the value of attribute blue.
13 14 15 |
# File 'lib/inker/color.rb', line 13 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
13 14 15 |
# File 'lib/inker/color.rb', line 13 def green @green end |
#red ⇒ Object
Returns the value of attribute red.
13 14 15 |
# File 'lib/inker/color.rb', line 13 def red @red end |
Instance Method Details
#==(color) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/inker/color.rb', line 31 def ==(color) self.red == color.red and self.green == color.green and self.blue == color.blue and self.alpha == color.alpha end |
#brightness ⇒ Integer
Calculate the brightness of a color.
78 79 80 |
# File 'lib/inker/color.rb', line 78 def brightness Color.brightness(@red, @green, @blue) end |
#dark? ⇒ Boolean
Returns a boolean which indicates if the color is dark.
109 110 111 |
# File 'lib/inker/color.rb', line 109 def dark? brightness < 128 end |
#hue ⇒ Integer
Calculate the HUE of a color.
102 103 104 |
# File 'lib/inker/color.rb', line 102 def hue Color.hue(@red, @green, @blue) end |
#light? ⇒ Boolean
Returns a boolean which indicates if the color is light.
115 116 117 |
# File 'lib/inker/color.rb', line 115 def light? !dark? end |
#lightness ⇒ Float
Calculate the lightness of a color.
86 87 88 |
# File 'lib/inker/color.rb', line 86 def lightness Color.lightness(@red, @green, @blue) end |
#saturation ⇒ Float
Calculate the saturation of a color.
94 95 96 |
# File 'lib/inker/color.rb', line 94 def saturation Color.saturation(@red, @green, @blue) end |
#to_s(format = 'hex') ⇒ String
Convert color to string in the specified format.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/inker/color.rb', line 125 def to_s(format = 'hex') case format.to_s.strip.downcase when 'hex6' then self.hex6 when 'rgb' then self.rgb when 'rgba' then self.rgba when 'hsl' then self.hsl when 'hsla' then self.hsla else self.hex end end |