Module: Chroma::Color::Serializers
- Included in:
- Chroma::Color
- Defined in:
- lib/chroma/color/serializers.rb
Overview
Methods for serializing Chroma::Color to different color mode string formats.
Instance Attribute Summary collapse
-
#rgb ⇒ ColorModes::Rgb
readonly
Converts to an instance of Chroma::ColorModes::Rgb.
Instance Method Summary collapse
-
#hsl ⇒ ColorModes::Hsl
Converts to an instance of Chroma::ColorModes::Hsl.
-
#hsv ⇒ ColorModes::Hsv
Converts to an instance of Chroma::ColorModes::Hsv.
-
#to_hex(allow_3 = false) ⇒ String
Convert to hexadecimal string.
-
#to_hex8 ⇒ String
Convert to 8-character hexadecimal string.
-
#to_hsl ⇒ String
Convert to hsl string.
-
#to_hsv ⇒ String
Convert to hsv string.
-
#to_name(hex_for_unknown = false) ⇒ String
Convert to named color if possible.
-
#to_rgb ⇒ String
Convert to rgb string.
-
#to_s(format = @format) ⇒ String
(also: #inspect)
Convert to a string based on the color format.
Instance Attribute Details
#rgb ⇒ ColorModes::Rgb (readonly)
Converts to an instance of Chroma::ColorModes::Rgb
136 137 138 |
# File 'lib/chroma/color/serializers.rb', line 136 def rgb @rgb end |
Instance Method Details
#hsl ⇒ ColorModes::Hsl
Converts to an instance of Chroma::ColorModes::Hsl
130 131 132 |
# File 'lib/chroma/color/serializers.rb', line 130 def hsl Converters::HslConverter.convert_rgb(@rgb) end |
#hsv ⇒ ColorModes::Hsv
Converts to an instance of Chroma::ColorModes::Hsv
124 125 126 |
# File 'lib/chroma/color/serializers.rb', line 124 def hsv Converters::HsvConverter.convert_rgb(@rgb) end |
#to_hex(allow_3 = false) ⇒ String
Convert to hexadecimal string.
37 38 39 |
# File 'lib/chroma/color/serializers.rb', line 37 def to_hex(allow_3 = false) "##{to_basic_hex(allow_3)}" end |
#to_hex8 ⇒ String
Convert to 8-character hexadecimal string. The highest order byte (left most hexadecimal pair represents the alpha value).
49 50 51 |
# File 'lib/chroma/color/serializers.rb', line 49 def to_hex8 "##{to_basic_hex8}" end |
#to_hsl ⇒ String
Convert to hsl string.
23 24 25 |
# File 'lib/chroma/color/serializers.rb', line 23 def to_hsl to_hs(:l) end |
#to_hsv ⇒ String
Convert to hsv string.
12 13 14 |
# File 'lib/chroma/color/serializers.rb', line 12 def to_hsv to_hs(:v) end |
#to_name(hex_for_unknown = false) ⇒ String
Convert to named color if possible. If a color name can't be found, it
returns '<unknown>'
or the hexadecimal string based on the value of
hex_for_unknown
.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chroma/color/serializers.rb', line 79 def to_name(hex_for_unknown = false) return 'transparent' if alpha.zero? if alpha < 1 || (name = Chroma.name_from_hex(to_basic_hex(true))).nil? if hex_for_unknown to_hex else '<unknown>' end else name end end |
#to_rgb ⇒ String
Convert to rgb string.
60 61 62 63 64 |
# File 'lib/chroma/color/serializers.rb', line 60 def to_rgb middle = @rgb.to_a[0..2].map(&:round).join(', ') with_alpha(:rgb, middle) end |
#to_s(format = @format) ⇒ String Also known as: inspect
Convert to a string based on the color format.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chroma/color/serializers.rb', line 103 def to_s(format = @format) use_alpha = alpha < 1 && alpha >= 0 && /^hex(3|6)?$/ =~ format return to_rgb if use_alpha case format.to_s when 'rgb' then to_rgb when 'hex', 'hex6' then to_hex when 'hex3' then to_hex(true) when 'hex8' then to_hex8 when 'hsl' then to_hsl when 'hsv' then to_hsv when 'name' then to_name(true) else to_hex end end |