Module: ImmosquareColors
- Defined in:
- lib/immosquare-colors.rb,
lib/immosquare-colors/version.rb
Constant Summary collapse
- VERSION =
"0.1.5".freeze
Class Method Summary collapse
-
.color_name_to_hex(color_name) ⇒ Object
## A dictionary for mapping textual colors to colors hex ============================================================##.
-
.get_complementary_color(color, options = {}) ⇒ Object
## To determine whether the complementary color should be black or white ============================================================##.
-
.hex_to_rgba(hex_color) ⇒ Object
## To transform a hex color to rgba ============================================================##.
-
.rgba_to_hex(rgba_color) ⇒ Object
## To transform a rgba color (array) to hex ============================================================##.
-
.shade_color(color, weight) ⇒ Object
## Mix color whith black (0, 0, 0) ============================================================##.
-
.tint_color(color, weight) ⇒ Object
## Mix color whith white (255, 255, 255) ============================================================##.
Class Method Details
.color_name_to_hex(color_name) ⇒ Object
##
A dictionary for mapping textual colors to colors hex
##
63 64 65 |
# File 'lib/immosquare-colors.rb', line 63 def color_name_to_hex(color_name) ImmosquareConstants::Color.color_name_to_hex(color_name.downcase.to_sym) || "#000000" end |
.get_complementary_color(color, options = {}) ⇒ Object
##
To determine whether the complementary color should be black or white
##
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/immosquare-colors.rb', line 10 def get_complementary_color(color, = {}) begin luminance_std = ([:luminance] || 127.5).to_f black = "#000000" white = "#FFFFFF" color_hex = color.start_with?("#") ? color : color_name_to_hex(color) raise("Not valid size") if ![7, 9].include?(color_hex.length) color_rgba = hex_to_rgba(color_hex) r, g, b, a = color_rgba ##============================================================## ## luminance calculation ##============================================================## luminance = Math.sqrt((0.299 * (r**2)) + (0.587 * (g**2)) + (0.114 * (b**2))) luminance > luminance_std ? black : white rescue StandardError => e puts("=== Error! ===") puts(e.) puts(e.backtrace) black end end |
.hex_to_rgba(hex_color) ⇒ Object
##
To transform a hex color to rgba
##
39 40 41 42 43 44 45 46 47 |
# File 'lib/immosquare-colors.rb', line 39 def hex_to_rgba(hex_color) hex_color = hex_color.gsub("#", "") red = hex_color[0..1].to_i(16) green = hex_color[2..3].to_i(16) blue = hex_color[4..5].to_i(16) rgba = [red, green, blue] rgba += [(hex_color[6..7].to_i(16) / 255.0).round(2)] if hex_color.length == 8 rgba end |
.rgba_to_hex(rgba_color) ⇒ Object
##
To transform a rgba color (array) to hex
##
52 53 54 55 56 57 |
# File 'lib/immosquare-colors.rb', line 52 def rgba_to_hex(rgba_color) r, g, b, a = rgba_color hex_string = format("#%02x%02x%02x", r, g, b) hex_string += format("%02x", (a * 255).floor) if a && a != 1.0 hex_string.upcase end |
.shade_color(color, weight) ⇒ Object
##
Mix color whith black (0, 0, 0)
##
82 83 84 85 86 87 88 89 |
# File 'lib/immosquare-colors.rb', line 82 def shade_color(color, weight) color_hex = color.start_with?("#") ? color : color_name_to_hex(color) r, g, b, a = hex_to_rgba(color_hex) shaded_r = (r * (1 - weight)).round shaded_g = (g * (1 - weight)).round shaded_b = (b * (1 - weight)).round rgba_to_hex([shaded_r, shaded_g, shaded_b, a]) end |
.tint_color(color, weight) ⇒ Object
##
Mix color whith white (255, 255, 255)
##
70 71 72 73 74 75 76 77 |
# File 'lib/immosquare-colors.rb', line 70 def tint_color(color, weight) color_hex = color.start_with?("#") ? color : color_name_to_hex(color) r, g, b, a = hex_to_rgba(color_hex) tinted_r = (((1 - weight) * r) + (weight * 255)).round tinted_g = (((1 - weight) * g) + (weight * 255)).round tinted_b = (((1 - weight) * b) + (weight * 255)).round rgba_to_hex([tinted_r, tinted_g, tinted_b, a]) end |