Module: Inker::Color::Serializers

Included in:
Inker::Color
Defined in:
lib/inker/color/serializers.rb

Overview

This module implements the methods which can be used to serialize a color as string in different formats.

Instance Method Summary collapse

Instance Method Details

#hex(force_alpha: false) ⇒ String

Convert color to a HEX color string.

Parameters:

  • force_alpha (Boolean) (defaults to: false)

    indicates if alpha channel should be included in HEX color string when alpha component wasn't specified

Returns:

  • (String)

    a HEX color string



13
14
15
16
17
18
# File 'lib/inker/color/serializers.rb', line 13

def hex(force_alpha: false)
  result = hex6
  result += (alpha * 255).to_i.to_s(16).rjust(2, '0') if alpha < 1 || force_alpha

  result
end

#hex6String

Convert color to a HEX color string without alpha channel.

Returns:

  • (String)

    a HEX color string without alpha channel



23
24
25
26
27
28
29
30
# File 'lib/inker/color/serializers.rb', line 23

def hex6
  result = "#"
  result += red.to_s(16).rjust(2, '0')
  result += green.to_s(16).rjust(2, '0')
  result += blue.to_s(16).rjust(2, '0')

  result
end

#hslString

Convert color to HSL color string.

Returns:

  • (String)

    a HSL color string



51
52
53
# File 'lib/inker/color/serializers.rb', line 51

def hsl
  "hsl(#{hue}, #{(saturation * 100).round}%, #{(lightness * 100).round}%)"
end

#hsla(alpha_precision: 2) ⇒ String

Convert color to HSL color string.

Parameters:

  • alpha_precision (Integer) (defaults to: 2)

    indicates the precision of alpha value

Returns:

  • (String)

    a HSL color string



60
61
62
# File 'lib/inker/color/serializers.rb', line 60

def hsla(alpha_precision: 2)
  "hsl(#{hue}, #{(saturation * 100).round}%, #{(lightness * 100).round}%, #{alpha.round(alpha_precision)})"
end

#rgbString

Convert color to RGB color string.

Returns:

  • (String)

    a RGB color string



35
36
37
# File 'lib/inker/color/serializers.rb', line 35

def rgb
  "rgb(#{red}, #{green}, #{blue})"
end

#rgba(alpha_precision: 2) ⇒ String

Convert color to RGBA color string.

Parameters:

  • alpha_precision (Integer) (defaults to: 2)

    indicates the precision of alpha value

Returns:

  • (String)

    a RGBA color string



44
45
46
# File 'lib/inker/color/serializers.rb', line 44

def rgba(alpha_precision: 2)
  "rgba(#{red}, #{green}, #{blue}, #{alpha.round(alpha_precision)})"
end