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

Instance Method Summary collapse

Instance Attribute Details

#rgbColorModes::Rgb (readonly)

Converts to an instance of Chroma::ColorModes::Rgb

Returns:



136
137
138
# File 'lib/chroma/color/serializers.rb', line 136

def rgb
  @rgb
end

Instance Method Details

#hslColorModes::Hsl

Converts to an instance of Chroma::ColorModes::Hsl

Returns:



130
131
132
# File 'lib/chroma/color/serializers.rb', line 130

def hsl
  Converters::HslConverter.convert_rgb(@rgb)
end

#hsvColorModes::Hsv

Converts to an instance of Chroma::ColorModes::Hsv

Returns:



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.

Examples:

'red'.paint.to_hex                  #=> '#ff0000'
'red'.paint.to_hex(true)            #=> '#f00'
'rgba(255, 0, 0, 0.5)'.paint.to_hex #=> '#ff0000'

Parameters:

  • allow_3 (true, false) (defaults to: false)

    output 3-character hexadecimal if possible

Returns:



37
38
39
# File 'lib/chroma/color/serializers.rb', line 37

def to_hex(allow_3 = false)
  "##{to_basic_hex(allow_3)}"
end

#to_hex8String

Convert to 8-character hexadecimal string. The highest order byte (left most hexadecimal pair represents the alpha value).

Examples:

'red'.paint.to_hex                  #=> '#ffff0000'
'rgba(255, 0, 0, 0.5)'.paint.to_hex #=> '#80ff0000'

Returns:



49
50
51
# File 'lib/chroma/color/serializers.rb', line 49

def to_hex8
  "##{to_basic_hex8}"
end

#to_hslString

Convert to hsl string.

Examples:

'red'.paint.to_hsl                  #=> 'hsl(0, 100%, 50%)'
'rgba(255, 0, 0, 0.5)'.paint.to_hsl #=> 'hsla(0, 100%, 50%, 0.5)'

Returns:



23
24
25
# File 'lib/chroma/color/serializers.rb', line 23

def to_hsl
  to_hs(:l)
end

#to_hsvString

Convert to hsv string.

Examples:

'red'.paint.to_hsv                  #=> 'hsv(0, 100%, 100%)'
'rgba(255, 0, 0, 0.5)'.paint.to_hsv #=> 'hsva(0, 100%, 100%, 0.5)'

Returns:



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.

Examples:

'red'.paint.to_name                  #=> 'red'
'rgba(255, 0, 0, 0.5)'.paint.to_name #=> '<unknown>'
'#00f'.paint.to_name                 #=> 'blue'
'#123'.paint.to_name(true)           #=> '#112233'

Parameters:

  • hex_for_unknown (true, false) (defaults to: false)

    determine how unknown color names should be returned

Returns:



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_rgbString

Convert to rgb string.

Examples:

'red'.paint.to_rgb                  #=> 'rgb(255, 0, 0)'
'rgba(255, 0, 0, 0.5)'.paint.to_rgb #=> 'rgb(255, 0, 0, 0.5)'

Returns:



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.

Examples:

'red'.paint.to_s             #=> 'red'
'rgb(255, 0, 0)'.paint.to_s  #=> 'rgb(255, 0, 0)'
'#f00'.paint.to_s            #=> '#f00'
'#80ff0000'.paint.to_s(:rgb) #=> 'rgba(255, 0, 0, 0.5)'

Parameters:

  • format (Symbol) (defaults to: @format)

    the color format

Returns:



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