Class: Colors
- Inherits:
-
Object
- Object
- Colors
- Defined in:
- lib/kang/colors.rb
Overview
color.rb - Generate randon color sets that are distinguishable
Copyright © 2014 Tony Doan <[email protected]>. All rights reserved.
This software is licensed as described in the file COPYING, which you should have received as part of this distribution. The terms are also available at github.com/tdoan/kang/tree/master/COPYING. If newer versions of this license are posted there, you may use a newer version instead, at your option.
Class Method Summary collapse
Instance Method Summary collapse
- #[](i) ⇒ Object
- #each(&block) ⇒ Object
- #hsv_to_rgb(h, s, v) ⇒ Object
-
#initialize(initial_size = nil, options = {}) ⇒ Colors
constructor
A new instance of Colors.
- #next ⇒ Object
Constructor Details
#initialize(initial_size = nil, options = {}) ⇒ Colors
Returns a new instance of Colors.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/kang/colors.rb', line 16 def initialize(initial_size=nil, ={}) = [:saturation,:value,:starting_hue,:range] = {saturation:0.3, value:0.8, :starting_hue=>rand, :range=>256} .keys.each do |k| raise ArgumentError, "Unknown option: #{k}" unless .include? k end .merge!() [:saturation, :value, :starting_hue].each do |k| raise ArgumentError, "The option \"#{k.to_s}\" must be between 0 and 1" if(([k] > 1) or ([k] < 0)) end @golden_ratio_conjugate = 0.618033988749895 @h = [:starting_hue] @saturation = [:saturation] @value = [:value] @colors = [] @range = [:range] unless initial_size.nil? initial_size.times do self.next end end end |
Class Method Details
.to_hex(rgb) ⇒ Object
39 40 41 |
# File 'lib/kang/colors.rb', line 39 def self.to_hex(rgb) rgb.collect{|n| n.to_s(16).upcase}.join end |
Instance Method Details
#[](i) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/kang/colors.rb', line 62 def [](i) if @colors.size-1 < i diff = i - @colors.size + 1 (diff).times do self.next end end @colors[i] end |
#each(&block) ⇒ Object
58 59 60 |
# File 'lib/kang/colors.rb', line 58 def each(&block) @colors.each(&block) end |
#hsv_to_rgb(h, s, v) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kang/colors.rb', line 43 def hsv_to_rgb(h, s, v) h_i = (h*6).to_i f = h*6 - h_i p = v * (1 - s) q = v * (1 - f*s) t = v * (1 - (1 - f) * s) r, g, b = v, t, p if h_i==0 r, g, b = q, v, p if h_i==1 r, g, b = p, v, t if h_i==2 r, g, b = p, q, v if h_i==3 r, g, b = t, p, v if h_i==4 r, g, b = v, p, q if h_i==5 [(r*@range).to_i, (g*@range).to_i, (b*@range).to_i] end |
#next ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/kang/colors.rb', line 72 def next @h += @golden_ratio_conjugate @h %= 1 c = hsv_to_rgb(@h, @saturation, @value) @colors << c c end |