Module: Cpalette
- Defined in:
- lib/cpalette.rb,
lib/cpalette/version.rb
Constant Summary collapse
- VERSION =
"0.0.6"
Class Method Summary collapse
-
.get_color_codes_in_rgb(h, s, l) ⇒ Object
Given Hue, Saturation and Lightness values return RGB values to it.
-
.hue2rgb(p, q, t) ⇒ Object
Converts hue to rgb based on following conditions.
-
.palette(size, options = {}) ⇒ Object
Takes an input parameter of size (no of colors and options hash) Returns equivalent number of random colors with HSL, RGB, HEX codes Max possible values for Hue: 360, Saturation: 100, Lightness: 100 Saturation and Lightness values tend to determine the extremes of a color Choosing a range of 60..90 tends to give optimum colors.
-
.rgb2hex(r, g, b) ⇒ Object
Converts RGB to HEX code values R, G, B are converted to string of base 16 and padded a 0 if length is less than 2 Assumes input r, g, b in the range of [0, 255] Returns a HEX value E.g Input - r: 255, g:255, b:255 => #FFFFFF.
Class Method Details
.get_color_codes_in_rgb(h, s, l) ⇒ Object
Given Hue, Saturation and Lightness values return RGB values to it. Function adopted from - easyrgb.com Assumes inputs: h, s, l are in the range of [0,1] Returns outputs: R, G, B valu in the range of [0,255] E.g. Inputs: h: 0.25, s: 0.89, l: 0.54 Output: r: 141, g: 242, b: 33
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cpalette.rb', line 11 def self.get_color_codes_in_rgb(h, s, l) if s == 0 r = g = b = l * 255 else if l < 0.5 y = l * (1.0 + s) else y = l + s - l * s end x = 2 * l - y r = (255 * hue2rgb(x, y, h + 1.0/3)).round(0) g = (255 * hue2rgb(x, y, h)).round(0) b = (255 * hue2rgb(x, y, h - 1.0/3)).round(0) end return "#{r},#{g},#{b}" end |
.hue2rgb(p, q, t) ⇒ Object
Converts hue to rgb based on following conditions
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cpalette.rb', line 29 def self.hue2rgb(p,q,t) t = t + 1 if t < 0 t = t -1 if t > 1 if 6 * t < 1 return p+(q-p)*6*t end if 2 * t < 1 return q end if 3 * t < 2 return p + (q - p) * ((2.0/3 - t) * 6) end return p end |
.palette(size, options = {}) ⇒ Object
Takes an input parameter of size (no of colors and options hash) Returns equivalent number of random colors with HSL, RGB, HEX codes Max possible values for Hue: 360, Saturation: 100, Lightness: 100 Saturation and Lightness values tend to determine the extremes of a color Choosing a range of 60..90 tends to give optimum colors
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cpalette.rb', line 62 def self.palette(size, = {}) color_palette = [] hue_array = [] hue = 359 size = size.to_i step_size = 360/size for i in 0..size-1 hue_array.push(hue) hue = hue - step_size end hue_array.shuffle! #Inplace hue_array.each do |hue| s = Random.new.rand(60..90) l = Random.new.rand(40..80) color_codes = Hash.new color_codes["hsl"] = "#{hue},#{s}%,#{l}%" color_codes["rgb"] = get_color_codes_in_rgb(hue/360.0,s/100.0,l/100.0) rgb = color_codes["rgb"].split(",") color_codes["hex"] = rgb2hex(rgb[0].to_i, rgb[1].to_i, rgb[2].to_i) color_palette.push(color_codes) unless color_palette.include?(color_codes) end # If only hex option is set by user if [:hex] != nil if [:hex] == true cpalette = [] color_palette.each do |color| cpalette.push(color["hex"]) end color_palette = cpalette end end # binding.pry return color_palette end |
.rgb2hex(r, g, b) ⇒ Object
Converts RGB to HEX code values R, G, B are converted to string of base 16 and padded a 0 if length is less than 2 Assumes input r, g, b in the range of [0, 255] Returns a HEX value E.g Input - r: 255, g:255, b:255 => #FFFFFF
50 51 52 53 54 55 |
# File 'lib/cpalette.rb', line 50 def self.rgb2hex(r, g, b) h1 = r.to_s(16).length == 2 ? r.to_s(16) : "0"+r.to_s(16) h2 = g.to_s(16).length == 2 ? g.to_s(16) : "0"+g.to_s(16) h3 = b.to_s(16).length == 2 ? b.to_s(16) : "0"+b.to_s(16) return "##{h1}#{h2}#{h3}" end |