Class: Paleta::Color
Overview
Represents a color
Instance Attribute Summary collapse
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#hex ⇒ Object
Returns the value of attribute hex.
-
#hue ⇒ Object
Returns the value of attribute hue.
-
#lightness ⇒ Object
Returns the value of attribute lightness.
-
#red ⇒ Object
Returns the value of attribute red.
-
#saturation ⇒ Object
Returns the value of attribute saturation.
Instance Method Summary collapse
-
#==(color) ⇒ Boolean
Determine the equality of the receiver and another Color.
-
#complement ⇒ Color
Create a new Color that is the complement of the receiver.
-
#complement! ⇒ Color
Turn the receiver into it’s complement.
-
#darken(percentage = 5) ⇒ Color
Create a copy of the receiver and darken it by a percentage.
-
#darken!(percentage = 5) ⇒ Color
Darken the receiver by a percentage.
-
#desaturate ⇒ Color
Create a copy of the receiver and desaturate it.
-
#desaturate! ⇒ Color
Desaturate the receiver.
-
#initialize(*args) ⇒ Color
constructor
Initailize a Color.
-
#invert ⇒ Color
Create a copy of the receiver and invert it.
-
#invert! ⇒ Color
Invert the receiver.
-
#lighten(percentage = 5) ⇒ Color
Create a copy of the receiver and lighten it by a percentage.
-
#lighten!(percentage = 5) ⇒ Color
Lighten the receiver by a percentage.
-
#similarity(color) ⇒ Number
Calculate the similarity between the receiver and another Color.
-
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Color instance,.
Methods included from Math
#distance, #multiple_regression
Constructor Details
#initialize ⇒ Color #initialize(color) ⇒ Color #initialize(model, value) ⇒ Color #initialize(model, value, value, value) ⇒ Color #initialize(value, value, value) ⇒ Color
Initailize a Paleta::Color
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/paleta/color.rb', line 38 def initialize(*args) if args.length == 1 && args[0].is_a?(Color) args[0].instance_variables.each do |key| self.send("#{key[1..key.length]}=".to_sym, args[0].send("#{key[1..key.length]}")) end elsif args.length == 2 && args[0] == :hex && args[1].is_a?(String) # example: new(:hex, "336699") # example: new(:hex, "#336699") # example: new(:hex, "#fff") color_hex = args[1] color_hex = color_hex[1..-1] if color_hex.start_with?('#') # These are all string operations to make a "fea" into a "ffeeaa" color_hex = color_hex[0] * 2 + color_hex[1] * 2 + color_hex[2] * 2 if color_hex.length == 3 hex_init(color_hex) elsif args.length == 3 && args[0].is_a?(Numeric) && args[1].is_a?(Numeric) && args[2].is_a?(Numeric) # example: new(235, 129, 74) rgb_init(args[0], args[1], args[2]) elsif args.length == 4 && [:rgb, :hsl].include?(args[0]) && args[1].is_a?(Numeric) && args[2].is_a?(Numeric) && args[3].is_a?(Numeric) # example: new(:hsl, 320, 96, 74) rgb_init(args[1], args[2], args[3]) if args[0] == :rgb # example: new(:rgb, 235, 129, 74) hsl_init(args[1], args[2], args[3]) if args[0] == :hsl elsif args.length == 0 # example: new() rgb_init(0, 0, 0) else raise(ArgumentError, "Invalid arguments") end end |
Instance Attribute Details
#blue ⇒ Object
Returns the value of attribute blue.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def green @green end |
#hex ⇒ Object
Returns the value of attribute hex.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def hex @hex end |
#hue ⇒ Object
Returns the value of attribute hue.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def hue @hue end |
#lightness ⇒ Object
Returns the value of attribute lightness.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def lightness @lightness end |
#red ⇒ Object
Returns the value of attribute red.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def red @red end |
#saturation ⇒ Object
Returns the value of attribute saturation.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def saturation @saturation end |
Instance Method Details
#==(color) ⇒ Boolean
Determine the equality of the receiver and another Paleta::Color
118 119 120 |
# File 'lib/paleta/color.rb', line 118 def ==(color) color.is_a?(Color) ? (self.hex == color.hex) : false end |
#complement ⇒ Color
Create a new Paleta::Color that is the complement of the receiver
200 201 202 203 204 |
# File 'lib/paleta/color.rb', line 200 def complement copy = self.class.new(self) copy.complement! copy end |
#complement! ⇒ Color
Turn the receiver into it’s complement
208 209 210 211 212 213 |
# File 'lib/paleta/color.rb', line 208 def complement! @hue = (@hue + 180) % 360 update_rgb update_hex self end |
#darken(percentage = 5) ⇒ Color
Create a copy of the receiver and darken it by a percentage
145 146 147 148 149 |
# File 'lib/paleta/color.rb', line 145 def darken(percentage = 5) copy = self.dup copy.darken!(percentage) copy end |
#darken!(percentage = 5) ⇒ Color
Darken the receiver by a percentage
154 155 156 157 158 159 160 |
# File 'lib/paleta/color.rb', line 154 def darken!(percentage = 5) @lightness -= percentage @lightness = 0 if @lightness < 0 update_rgb update_hex self end |
#desaturate ⇒ Color
Create a copy of the receiver and desaturate it
183 184 185 186 187 |
# File 'lib/paleta/color.rb', line 183 def desaturate copy = self.class.new(self) copy.desaturate! copy end |
#desaturate! ⇒ Color
Desaturate the receiver
191 192 193 194 195 196 |
# File 'lib/paleta/color.rb', line 191 def desaturate! @saturation = 0 update_rgb update_hex self end |
#invert ⇒ Color
Create a copy of the receiver and invert it
164 165 166 167 168 |
# File 'lib/paleta/color.rb', line 164 def invert copy = self.class.new(self) copy.invert! copy end |
#invert! ⇒ Color
Invert the receiver
172 173 174 175 176 177 178 179 |
# File 'lib/paleta/color.rb', line 172 def invert! @red = 255 - @red @green = 255 - @green @blue = 255 - @blue update_hsl update_hex self end |
#lighten(percentage = 5) ⇒ Color
Create a copy of the receiver and lighten it by a percentage
125 126 127 128 129 |
# File 'lib/paleta/color.rb', line 125 def lighten(percentage = 5) copy = self.dup copy.lighten!(percentage) copy end |
#lighten!(percentage = 5) ⇒ Color
Lighten the receiver by a percentage
134 135 136 137 138 139 140 |
# File 'lib/paleta/color.rb', line 134 def lighten!(percentage = 5) @lightness += percentage @lightness = 100 if @lightness > 100 update_rgb update_hex self end |
#similarity(color) ⇒ Number
Calculate the similarity between the receiver and another Paleta::Color
218 219 220 |
# File 'lib/paleta/color.rb', line 218 def similarity(color) distance({ :r => @red, :g => @green, :b => @blue}, { :r => color.red, :g => color.green, :b => color.blue}) / sqrt(3 * (255 ** 2)) end |
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Paleta::Color instance,
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/paleta/color.rb', line 225 def to_array(color_model = :rgb) color_model = color_model.to_sym unless color_model.is_a? Symbol if color_model == :rgb array = [self.red, self.green, self.blue] elsif color_model == :hsl array = [self.hue, self.saturation, self.lightness] else raise(ArgumentError, "Argument must be :rgb or :hsl") end array end |