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



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

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

  return 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")

  return result
end

#hslString

Convert color to HSL color string.

Returns:

  • (String)

    a HSL color string



54
55
56
# File 'lib/inker/color/serializers.rb', line 54

def hsl
  return "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



64
65
66
# File 'lib/inker/color/serializers.rb', line 64

def hsla(alpha_precision: 2)
  return "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



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

def rgb
  return "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



46
47
48
# File 'lib/inker/color/serializers.rb', line 46

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