Class: Osb::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/osb/color.rb

Overview

Represents an RGB color.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, g = nil, b = nil) ⇒ Color

Returns a new instance of Color.

Parameters:

  • r (Integer, String, Array<Integer>)

    red value, a hex String, or an Array of 3 {Integer}s.

  • g (Integer) (defaults to: nil)

    green value

  • b (Integer) (defaults to: nil)

    blue value



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osb/color.rb', line 18

def initialize(r, g = nil, b = nil)
  Internal.assert_type!(
    r,
    [Integer, String, Internal::T[Array][Integer]],
    "r"
  )
  if r.is_a?(Array)
    if r.size != 3
      raise InvalidValueError, "Must be an Array of 3 Integers."
    end
    @r = r[0]
    @g = r[1]
    @b = r[2]
  elsif r.is_a?(String)
    Color.from_hex(r)
  else
    Internal.assert_value!(r, 0..255, "r")

    Internal.assert_type!(g, Integer, "g")
    Internal.assert_value!(g, 0..255, "g")

    Internal.assert_type!(b, Integer, "b")
    Internal.assert_value!(b, 0..255, "b")

    @r = r
    @g = g
    @b = b
  end
end

Instance Attribute Details

#bObject

Returns Blue value.

Returns:

  • Blue value.



7
8
9
# File 'lib/osb/color.rb', line 7

def b
  @b
end

#gObject

Returns Green value.

Returns:

  • Green value.



7
8
9
# File 'lib/osb/color.rb', line 7

def g
  @g
end

#rObject

Returns Red value.

Returns:

  • Red value.



7
8
9
# File 'lib/osb/color.rb', line 7

def r
  @r
end

Class Method Details

.from_hex(hex) ⇒ Color

Create a {Color} object from hex string.

Parameters:

  • hex (String)

Returns:



106
107
108
109
110
111
112
# File 'lib/osb/color.rb', line 106

def self.from_hex(hex)
  Internal.assert_type!(hex, String, "hex")

  hex.gsub!("#", "")
  components = hex.scan(/.{2}/)
  components.collect { |component| component.to_i(16) }
end

.from_hsl(h, s, l) ⇒ Color

Converts an HSL color value to RGB.

Parameters:

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/osb/color.rb', line 61

def self.from_hsl(h, s, l)
  Internal.assert_type!(h, Integer, "h")
  Internal.assert_type!(s, Integer, "s")
  Internal.assert_type!(l, Integer, "l")

  h = h / 360.0
  s = s / 100.0
  l = l / 100.0

  r = 0.0
  g = 0.0
  b = 0.0

  if s == 0.0
    r = l.to_f
    g = l.to_f
    b = l.to_f
  else
    q = l < 0.5 ? l * (1 + s) : l + s - l * s
    p = 2 * l - q
    r = hue_to_rgb(p, q, h + 1 / 3.0)
    g = hue_to_rgb(p, q, h)
    b = hue_to_rgb(p, q, h - 1 / 3.0)
  end

  Color.new((r * 255).round, (g * 255).round, (b * 255).round)
end

Instance Method Details

#!=(color) ⇒ Object

Returns whether 2 colors are not equal.

Parameters:



50
51
52
53
54
# File 'lib/osb/color.rb', line 50

def !=(color)
  Internal.assert_type!(color, Color, "color")

  color.r != self.r && color.g != self.g && color.b != self.b
end