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
-
#hex(force_alpha: false) ⇒ String
Convert color to a HEX color string.
-
#hex6 ⇒ String
Convert color to a HEX color string without alpha channel.
-
#hsl ⇒ String
Convert color to HSL color string.
-
#hsla(alpha_precision: 2) ⇒ String
Convert color to HSL color string.
-
#rgb ⇒ String
Convert color to RGB color string.
-
#rgba(alpha_precision: 2) ⇒ String
Convert color to RGBA color string.
Instance Method Details
#hex(force_alpha: false) ⇒ String
Convert color to 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 |
#hex6 ⇒ String
Convert color to 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 |
#hsl ⇒ String
Convert color to 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.
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 |
#rgb ⇒ String
Convert color to 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.
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 |