Class: Inker::Color

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

Instance Method Summary collapse

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.

Parameters:

  • color_str (String)

    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

#alphaObject

Returns the value of attribute alpha.



13
14
15
# File 'lib/inker/color.rb', line 13

def alpha
  @alpha
end

#blueObject

Returns the value of attribute blue.



13
14
15
# File 'lib/inker/color.rb', line 13

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



13
14
15
# File 'lib/inker/color.rb', line 13

def green
  @green
end

#redObject

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

#brightnessInteger

Calculate the brightness of a color.

Returns:

  • (Integer)

    a value between 0-255 which indicates the brightness of the color



103
104
105
# File 'lib/inker/color.rb', line 103

def brightness
  Color.brightness(@red, @green, @blue)
end

#componentsArray<Integer> Also known as: to_a

Get RGBA color components.

Returns:

  • (Array<Integer>)

    an array of RGBA color components [red, green, blue, alpha].



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.

Parameters:

  • a (Float)

    value between 0.0-21.0 which indicates the contrast ratio between two colors (higher is better).



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.

Returns:

  • (Boolean)

    true when 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

#hueInteger

Calculate the HUE of a color.

Returns:

  • (Integer)

    a value between 0-360 which indicates the HUE of the 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.

Returns:

  • (Boolean)

    true when color is light



137
138
139
# File 'lib/inker/color.rb', line 137

def light?
  !dark?
end

#lightnessFloat

Calculate the lightness of a color.

Returns:

  • (Float)

    a value between 0.0-1.0 which indicates the lightness of the color



110
111
112
# File 'lib/inker/color.rb', line 110

def lightness
  Color.lightness(@red, @green, @blue)
end

#luminanceFloat

Returns the luminance of the color.

Returns:

  • (Float)

    a value between 0.0-1.0 which indicates 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.

Returns:



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 overlay(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

#saturationFloat

Calculate the saturation of a color.

Returns:

  • (Float)

    a value between 0.0-1.0 which indicates the saturation of the color



117
118
119
# File 'lib/inker/color.rb', line 117

def saturation
  Color.saturation(@red, @green, @blue)
end

#to_colorObject



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.

Parameters:

  • format (String) (defaults to: 'hex')

    indicates the format to which to output the color (default: hex)

Returns:

  • (String)

    a string representation of the color



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