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
- #==(other) ⇒ Object
-
#[](key) ⇒ Object
Get a RGBA component by index (0 => red, 1 => green, 2 => blue, 3 => alpha).
-
#[]=(key, value) ⇒ Object
Set a RGBA component by index (0 => red, 1 => green, 2 => blue, 3 => alpha).
-
#brightness ⇒ Integer
Calculate the brightness of a color.
-
#components ⇒ Array<Integer>
(also: #to_a)
Get RGBA color components.
-
#contrast_ratio(color) ⇒ Object
Returns the contrast ratio between two colors.
-
#dark? ⇒ Boolean
Returns a boolean which indicates if the color is dark.
-
#each(&block) ⇒ Object
Iterate over RGBA color components.
-
#each_with_index(&block) ⇒ Object
Iterate over RGBA color components with index.
-
#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.
-
#luminance ⇒ Float
Returns the luminance of the color.
-
#overlay(color) ⇒ Inker::Color
Calculates the result of 2 colors overlay.
-
#saturation ⇒ Float
Calculate the saturation of a color.
- #to_color ⇒ Object
-
#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, hex?, hsl?, hsl_to_rgb, hsla?, parse_color, random, rgb?, rgba?
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!(input) 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
#==(other) ⇒ Object
31 32 33 |
# File 'lib/inker/color.rb', line 31 def ==(other) red == other.red && green == other.green && blue == other.blue && alpha == other.alpha end |
#[](key) ⇒ Object
Get a RGBA component by index (0 => red, 1 => green, 2 => blue, 3 => alpha).
44 45 46 |
# File 'lib/inker/color.rb', line 44 def [](key) components[key] end |
#[]=(key, value) ⇒ Object
Set a RGBA component by index (0 => red, 1 => green, 2 => blue, 3 => alpha).
49 50 51 52 |
# File 'lib/inker/color.rb', line 49 def []=(key, value) component_name = %i[red green blue alpha][key] send("#{component_name}=", value) end |
#brightness ⇒ Integer
Calculate the brightness of a color.
103 104 105 |
# File 'lib/inker/color.rb', line 103 def brightness Color.brightness(@red, @green, @blue) end |
#components ⇒ Array<Integer> Also known as: to_a
Get RGBA color components.
37 38 39 |
# File 'lib/inker/color.rb', line 37 def components [red, green, blue, alpha] end |
#contrast_ratio(color) ⇒ Object
Returns the contrast ratio between two colors.
169 170 171 172 173 |
# File 'lib/inker/color.rb', line 169 def contrast_ratio(color) l2, l1 = [luminance, color.to_color.luminance].minmax (l1 + 0.05) / (l2 + 0.05) end |
#dark? ⇒ Boolean
Returns a boolean which indicates if the color is dark.
131 132 133 |
# File 'lib/inker/color.rb', line 131 def dark? brightness < 128 end |
#each(&block) ⇒ Object
Iterate over RGBA color components.
55 56 57 |
# File 'lib/inker/color.rb', line 55 def each(&block) components.each(&block) end |
#each_with_index(&block) ⇒ Object
Iterate over RGBA color components with index.
60 61 62 |
# File 'lib/inker/color.rb', line 60 def each_with_index(&block) components.each_with_index(&block) end |
#hue ⇒ Integer
Calculate the HUE of a color.
124 125 126 |
# File 'lib/inker/color.rb', line 124 def hue Color.hue(@red, @green, @blue) end |
#light? ⇒ Boolean
Returns a boolean which indicates if the color is light.
137 138 139 |
# File 'lib/inker/color.rb', line 137 def light? !dark? end |
#lightness ⇒ Float
Calculate the lightness of a color.
110 111 112 |
# File 'lib/inker/color.rb', line 110 def lightness Color.lightness(@red, @green, @blue) end |
#luminance ⇒ Float
Returns the luminance of the color.
143 144 145 |
# File 'lib/inker/color.rb', line 143 def luminance Color.luminance(@red, @green, @blue) end |
#overlay(color) ⇒ Inker::Color
Calculates the result of 2 colors overlay.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/inker/color.rb', line 149 def (color) result = dup other = color.to_color return result if alpha >= 1 alpha_weight = other.alpha * (1 - alpha) # Calculate the result of the overlay operation. 3.times do |i| result[i] = (self[i] * alpha + other[i] * alpha_weight).round end result.alpha = alpha + alpha_weight result end |
#saturation ⇒ Float
Calculate the saturation of a color.
117 118 119 |
# File 'lib/inker/color.rb', line 117 def saturation Color.saturation(@red, @green, @blue) end |
#to_color ⇒ Object
192 193 194 |
# File 'lib/inker/color.rb', line 192 def to_color self end |
#to_s(format = 'hex') ⇒ String
Convert color to string in the specified format.
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/inker/color.rb', line 180 def to_s(format = 'hex') case format.to_s.strip.downcase when 'hex6' then hex6 when 'rgb' then rgb when 'rgba' then rgba when 'hsl' then hsl when 'hsla' then hsla else hex end end |