Class: Paleta::Palette
- Inherits:
-
Object
- Object
- Paleta::Palette
- Includes:
- Enumerable, Math, MagickDependent
- Defined in:
- lib/paleta/palette.rb
Overview
Represents a palette, a collection of Colors
Instance Attribute Summary collapse
-
#colors ⇒ Object
Returns the value of attribute colors.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Palette
-
#[](index) ⇒ Object
Access a Color in the receiver by index.
-
#darken!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage.
-
#delete_at(index = 0) ⇒ Object
Remove a Color from the receiver by index.
- #each ⇒ Object
-
#include?(color) ⇒ Boolean
Test if a Color exists in the receiver.
- #initialize(*args) ⇒ Palette constructor
-
#invert! ⇒ Palette
Invert each Color in the receiver by a percentage.
-
#lighten!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage.
-
#pop ⇒ Object
Remove the most recently added Color from the receiver.
- #push(obj) ⇒ Palette
-
#similarity(palette) ⇒ Number
Calculate the similarity between the receiver and another Palette.
- #size ⇒ Number
-
#sort(&blk) ⇒ Palette
Create a new instance of Palette that is a sorted copy of the receiver.
-
#sort!(&blk) ⇒ Object
Sort the Colors in the receiver return [Palette] self.
-
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Palette instance,.
Methods included from MagickDependent
Methods included from Math
#distance, #multiple_regression
Constructor Details
#initialize(*args) ⇒ Palette
Initialize a Paleta::Palette from a list of Colors
42 43 44 45 46 |
# File 'lib/paleta/palette.rb', line 42 def initialize(*args) @colors = [] colors = (args.length == 1 && args[0].is_a?(Array)) ? args[0] : args colors.each { |color| self << color } end |
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
37 38 39 |
# File 'lib/paleta/palette.rb', line 37 def colors @colors end |
Class Method Details
.generate(opts = {}) ⇒ Palette
Generate a Paleta::Palette from a seed Color
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/paleta/palette.rb', line 183 def self.generate(opts = {}) size = opts[:size] || 5 if !opts[:type].nil? && opts[:type].to_sym == :random return self.generate_random_from_color(opts[:color], size) end unless (opts[:from].to_sym == :color && !opts[:color].nil?) || (opts[:from].to_sym == :image && !opts[:image].nil?) return raise(ArgumentError, 'You must pass :from and it must be either :color or :image, then you must pass :image => "/path/to/img" or :color => color') end if opts[:from].to_sym == :image path = opts[:image] return self.generate_from_image(path, size) end color = opts[:color] type = opts[:type] || :shades case type when :analogous; self.generate_analogous_from_color(color, size) when :complementary; self.generate_complementary_from_color(color, size) when :monochromatic; self.generate_monochromatic_from_color(color, size) when :shades; self.generate_shades_from_color(color, size) when :split_complement; self.generate_split_complement_from_color(color, size) when :tetrad; self.generate_tetrad_from_color(color, size) when :triad; self.generate_triad_from_color(color, size) else raise(ArgumentError, "Palette type is not defined. Try :analogous, :monochromatic, :shades, or :random") end end |
Instance Method Details
#<<(color) ⇒ Palette #<<(palette) ⇒ Palette
Add a Color to the Paleta::Palette
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/paleta/palette.rb', line 55 def <<(obj) if obj.is_a?(Color) @colors << obj elsif obj.is_a?(Palette) @colors |= obj.colors else raise(ArgumentError, "Passed argument is not a Color") end self end |
#[](index) ⇒ Object
Access a Color in the receiver by index
90 91 92 |
# File 'lib/paleta/palette.rb', line 90 def [](index) @colors[index] end |
#darken!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage
137 138 139 140 |
# File 'lib/paleta/palette.rb', line 137 def darken!(percentage = 5) @colors.each { |color| color.darken!(percentage) } self end |
#delete_at(index = 0) ⇒ Object
Remove a Color from the receiver by index
84 85 86 |
# File 'lib/paleta/palette.rb', line 84 def delete_at(index = 0) @colors.delete_at(index) end |
#each ⇒ Object
Iterate through each Color in the Paleta::Palette
101 102 103 |
# File 'lib/paleta/palette.rb', line 101 def each @colors.each { |c| yield c } end |
#include?(color) ⇒ Boolean
Test if a Color exists in the receiver
122 123 124 |
# File 'lib/paleta/palette.rb', line 122 def include?(color) @colors.include?(color) end |
#invert! ⇒ Palette
Invert each Color in the receiver by a percentage
144 145 146 147 |
# File 'lib/paleta/palette.rb', line 144 def invert! @colors.each { |color| color.invert! } self end |
#lighten!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage
129 130 131 132 |
# File 'lib/paleta/palette.rb', line 129 def lighten!(percentage = 5) @colors.each { |color| color.lighten!(percentage) } self end |
#pop ⇒ Object
Remove the most recently added Color from the receiver
78 79 80 |
# File 'lib/paleta/palette.rb', line 78 def pop @colors.pop end |
#push(color) ⇒ Palette #push(palette) ⇒ Palette
Add a Color to the Paleta::Palette
73 74 75 |
# File 'lib/paleta/palette.rb', line 73 def push(obj) self << obj end |
#similarity(palette) ⇒ Number
Calculate the similarity between the receiver and another Paleta::Palette
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/paleta/palette.rb', line 152 def similarity(palette) r, a, b = [], [], [] (0..1).each { |i| a[i], b[i] = {}, {} } # r[i] is a hash of the multiple regression of the Palette in RGB space r[0] = fit r[1] = palette.fit [0, 1].each do |i| [:x, :y, :z].each do |k| a[i][k] = 0 * r[i][:slope][k] + r[i][:offset][k] b[i][k] = 255 * r[i][:slope][k] + r[i][:offset][k] end end d_max = sqrt(3 * (65025 ** 2)) d1 = distance(a[0], a[1]) / d_max d2 = distance(b[0], b[1]) / d_max d1 + d2 end |
#size ⇒ Number
The number of Colors in the Paleta::Palette
96 97 98 |
# File 'lib/paleta/palette.rb', line 96 def size @colors.size end |
#sort(&blk) ⇒ Palette
Create a new instance of Paleta::Palette that is a sorted copy of the receiver
107 108 109 110 |
# File 'lib/paleta/palette.rb', line 107 def sort(&blk) @colors.sort(&blk) Paleta::Palette.new(@colors) end |
#sort!(&blk) ⇒ Object
Sort the Colors in the receiver return [Palette] self
114 115 116 117 |
# File 'lib/paleta/palette.rb', line 114 def sort!(&blk) @colors.sort!(&blk) self end |
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Paleta::Palette instance,
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/paleta/palette.rb', line 218 def to_array(color_model = :rgb) color_model = color_model.to_sym unless color_model.is_a? Symbol if [:rgb, :hsl].include?(color_model) array = colors.map { |c| c.to_array(color_model) } elsif color_model == :hex array = colors.map{ |c| c.hex } else raise(ArgumentError, "Argument must be :rgb, :hsl, or :hex") end array end |