Class: WDD::Utils::RGBColor
- Defined in:
- lib/wdd-ruby-ext/utils/rgb_color.rb
Instance Attribute Summary collapse
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#red ⇒ Object
Returns the value of attribute red.
Class Method Summary collapse
Instance Method Summary collapse
-
#darken(percentage) ⇒ Object
Darkens the color by
percentage
. - #desaturate(percentage) ⇒ Object
-
#initialize(*args) ⇒ RGBColor
constructor
A new instance of RGBColor.
-
#lighten(percentage) ⇒ Object
Lightens the color by
percentage
. - #saturate(percentage) ⇒ Object
- #saturation ⇒ Object
- #to_hex ⇒ Object
- #to_hex! ⇒ Object
Constructor Details
#initialize(*args) ⇒ RGBColor
Returns a new instance of RGBColor.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 13 def initialize( *args ) if args.length == 0 raise "Must supply initializer arguments" elsif args.length ==1 raise "Single argument must be a string" if !args[0].is_a?( String ) hex_color_string = args[0] if matches = hex_color_string.match( /#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/ ) @red = ("%02d" % "0x#{matches[1]}").to_i @green = ("%02d" % "0x#{matches[2]}").to_i @blue = ("%02d" % "0x#{matches[3]}").to_i elsif matches = hex_color_string.match( /#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})/ ) @red = ("%02d" % "0x#{matches[1]}#{matches[1]}").to_i @green = ("%02d" % "0x#{matches[2]}#{matches[2]}").to_i @blue = ("%02d" % "0x#{matches[3]}#{matches[3]}").to_i else raise "Invalid RGB hex color code #{hex_color_string}" if !matches end elsif args.length == 3 args.each do |arg| arg = arg.to_i raise "Invalid color value #{arg}" if !arg.is_a?( Fixnum ) || arg<0 || arg > 255 end @red = args[0] @green = args[1] @blue = args[2] end end |
Instance Attribute Details
#blue ⇒ Object
Returns the value of attribute blue.
11 12 13 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 11 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
10 11 12 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 10 def green @green end |
#red ⇒ Object
Returns the value of attribute red.
9 10 11 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 9 def red @red end |
Class Method Details
.color_cycle(start, arc_step_in_degrees, iterations, desaturation = 0, darken = 0) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 112 def self.color_cycle start, arc_step_in_degrees, iterations, desaturation=0, darken=0 angle = 0 two_thirds_PI = 2.0 * Math::PI / 3 four_thirds_PI = 4.0 * Math::PI / 3 half_PI = Math::PI / 2 colors = [] iterations.times do |i| angle = ((start + i*arc_step_in_degrees) * Math::PI * 2) / 360 red = (Math.cos(angle) + 1) / 2 * 255 green = (Math.cos(angle + two_thirds_PI) + 1) / 2 * 255 blue = (Math.cos(angle + four_thirds_PI) +1) / 2 * 255 desat = (Math.cos(i)+1) / 2 * 60 dark = (Math.cos(i*2)+1) / 2 * 20 color = RGBColor.new(red, green, blue).saturate(desat).darken(dark).desaturate(desaturation).darken(darken) colors << color yield color if block_given? end colors end |
Instance Method Details
#darken(percentage) ⇒ Object
Darkens the color by percentage
61 62 63 64 65 66 67 68 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 61 def darken percentage raise "Invalid multiplier #{multiplier}. Should be >= 0 and <= 100." if percentage < 0 || percentage > 100 multiplier = percentage.to_f / 100 new_red = (@red-multiplier*@red).min(255).to_i new_green = (@green-multiplier*@green).min(255).to_i new_blue = (@blue-multiplier*@blue).min(255).to_i return RGBColor.new( new_red, new_green, new_blue ) end |
#desaturate(percentage) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 97 def desaturate percentage multiplier = (100.0 - percentage.to_f) / 100 most_val = @red.max(@green).max(@blue) least_val = @red.min(@green).min(@blue) range = most_val red_ratio = 1.0 - @red.to_f / most_val green_ratio = 1.0 - @green.to_f / most_val blue_ratio = 1.0 - @blue.to_f / most_val highest_ratio = red_ratio.max(green_ratio).max(blue_ratio) new_red = most_val - red_ratio * range * multiplier new_green = most_val - green_ratio * range * multiplier new_blue = most_val - blue_ratio * range * multiplier return RGBColor.new( new_red.to_i, new_green.to_i, new_blue.to_i ) end |
#lighten(percentage) ⇒ Object
Lightens the color by percentage
51 52 53 54 55 56 57 58 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 51 def lighten percentage raise "Invalid percentage #{percentage}. Should be >= 0 and <= 100." if percentage < 0 || percentage > 100 multiplier = percentage.to_f / 100 new_red = (@red+(multiplier*(255-@red))).min(255).to_i new_green = (@green+(multiplier*(255-@green))).min(255).to_i new_blue = (@blue+(multiplier*(255-@blue))).min(255).to_i return RGBColor.new( new_red, new_green, new_blue ) end |
#saturate(percentage) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 76 def saturate percentage min_val = 0 multiplier = percentage.to_f / 100 most_val = @red.max(@green).max(@blue) return self.dup if most_val <= min_val least_val = @red.min(@green).min(@blue) red_ratio = 1.0 - @red.to_f / most_val green_ratio = 1.0 - @green.to_f / most_val blue_ratio = 1.0 - @blue.to_f / most_val new_red = @red - red_ratio * (@red - min_val) * multiplier new_green = @green - green_ratio * (@green - min_val) * multiplier new_blue = @blue - blue_ratio * (@blue - min_val) * multiplier # puts "@red: #{@red}" # puts "@green: #{@green}" # puts "@blue: #{@blue}" # puts "new_red: #{new_red}" # puts "new_green: #{new_green}" # puts "new_blue: #{new_blue}" return RGBColor.new( new_red.to_i, new_green.to_i, new_blue.to_i ) end |
#saturation ⇒ Object
70 71 72 73 74 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 70 def saturation least_val = @red.min(@green).min(@blue) most_val = @red.max(@green).max(@blue) saturation_level = (most_val - least_val).to_f / most_val * 100 end |
#to_hex ⇒ Object
42 43 44 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 42 def to_hex ("%02x" % @red) + ("%02x" % @green) + ("%02x" % @blue) end |
#to_hex! ⇒ Object
46 47 48 |
# File 'lib/wdd-ruby-ext/utils/rgb_color.rb', line 46 def to_hex! '#' + to_hex end |