Class: Colorspace::SRGBColor

Inherits:
Object
  • Object
show all
Defined in:
lib/colorspace/colorlib.rb

Overview

An RGB value in the sRGB colorspace.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hex(hex) ⇒ Object



73
74
75
76
77
78
# File 'lib/colorspace/colorlib.rb', line 73

def self.from_hex(hex)
  r, g, b = hex.chars.last(6).each_slice(2)
    .map { |c| c.join.to_i(16) / 255.0 }

  new(r: r || 0.0, g: g || 0.0, b: b || 0.0)
end

Instance Method Details

#gamma_expandObject



98
99
100
101
102
103
104
# File 'lib/colorspace/colorlib.rb', line 98

def gamma_expand
  SRGBLinearColor.new(
    r: gamma_expand_one(r),
    g: gamma_expand_one(g),
    b: gamma_expand_one(b)
  )
end

#to_aObject



107
# File 'lib/colorspace/colorlib.rb', line 107

def to_a = to_ary

#to_aryObject



106
# File 'lib/colorspace/colorlib.rb', line 106

def to_ary = [r, g, b]

#to_hexObject



80
81
82
83
84
# File 'lib/colorspace/colorlib.rb', line 80

def to_hex
  [r, g, b].map { |val|
    (val * 255).round.clamp(0, 255).to_s(16).rjust(2, '0')
  }.join
end

#to_xyzObject



94
95
96
# File 'lib/colorspace/colorlib.rb', line 94

def to_xyz
  gamma_expand.to_xyz
end