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, 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.

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!
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

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

#brightnessInteger

Calculate the brightness of a color.

Returns:

  • (Integer)

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

Returns:

  • (Boolean)

    true when color is dark



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

def dark?
  brightness < 128
end

#hueInteger

Calculate the HUE of a color.

Returns:

  • (Integer)

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

Returns:

  • (Boolean)

    true when color is light



115
116
117
# File 'lib/inker/color.rb', line 115

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



86
87
88
# File 'lib/inker/color.rb', line 86

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

#saturationFloat

Calculate the saturation of a color.

Returns:

  • (Float)

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

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



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