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.
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 |
#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") return result end |
#hsl ⇒ String
Convert color to 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.
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 |
#rgb ⇒ String
Convert color to 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.
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 |