Class: Falu::Palette
- Inherits:
-
Object
- Object
- Falu::Palette
- Includes:
- Canfig::Instance, Enumerable
- Defined in:
- lib/falu/palette.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#swatches ⇒ Object
readonly
Returns the value of attribute swatches.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(swatch) ⇒ Object
- #accent ⇒ Object
- #accent=(acc) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #blues(*args) ⇒ Object
- #darkest(*args) ⇒ Object
- #dominant(*args) ⇒ Object
- #each(&block) ⇒ Object
- #greens(*args) ⇒ Object
-
#initialize(swatches = nil, **opts) ⇒ Palette
constructor
A new instance of Palette.
- #lightest(*args) ⇒ Object
- #map(&block) ⇒ Object
- #primary ⇒ Object
- #primary=(pri) ⇒ Object
- #reds(*args) ⇒ Object
- #reverse! ⇒ Object
- #secondary ⇒ Object
- #secondary=(sec) ⇒ Object
- #sort!(*args, &block) ⇒ Object
- #sort_by!(*args, &block) ⇒ Object
- #sum(*args, &block) ⇒ Object
Constructor Details
#initialize(swatches = nil, **opts) ⇒ Palette
Returns a new instance of Palette.
20 21 22 23 24 25 26 27 28 |
# File 'lib/falu/palette.rb', line 20 def initialize(swatches=nil, **opts) @swatches = (swatches || []).map { |swatch| swatch.is_a?(Falu::Swatch) ? swatch : Falu::Swatch.new(*swatch) } configuration.configure(opts.slice!(:primary, :secondary, :accent)) self.primary = opts[:primary] self.secondary = opts[:secondary] self.accent = opts[:accent] end |
Instance Attribute Details
#swatches ⇒ Object (readonly)
Returns the value of attribute swatches.
6 7 8 |
# File 'lib/falu/palette.rb', line 6 def swatches @swatches end |
Class Method Details
.dump(palette) ⇒ Object
9 10 11 |
# File 'lib/falu/palette.rb', line 9 def dump(palette) palette.as_json end |
.load(json) ⇒ Object
13 14 15 16 17 |
# File 'lib/falu/palette.rb', line 13 def load(json) return if json.nil? json.symbolize_keys! new(json.delete(:swatches), **json) end |
Instance Method Details
#<<(swatch) ⇒ Object
42 43 44 |
# File 'lib/falu/palette.rb', line 42 def <<(swatch) swatches << (swatch.is_a?(Falu::Swatch) ? swatch : Falu::Swatch.new(*swatch)) end |
#accent ⇒ Object
136 137 138 |
# File 'lib/falu/palette.rb', line 136 def accent @accent ||= dominant(3).find { |swtch| ![primary.color.to_s, secondary.color.to_s].include?(swtch.color.to_s) } end |
#accent=(acc) ⇒ Object
131 132 133 134 |
# File 'lib/falu/palette.rb', line 131 def accent=(acc) # TODO check that the color exists in the palette @accent = acc end |
#as_json(options = {}) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/falu/palette.rb', line 140 def as_json(={}) { primary: primary.to_s, secondary: secondary.to_s, accent: accent.to_s, swatches: swatches.as_json } end |
#blues(*args) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/falu/palette.rb', line 97 def blues(*args) #swatches.sort_by { |swatch| swatch.color.rgb.blue }.reverse.first(*args) swatches.sort do |a,b| ablu = a.color.rgb.blue - (a.color.rgb.red + a.color.rgb.green) bblu = b.color.rgb.blue - (b.color.rgb.red + b.color.rgb.green) ablu <=> bblu end.first(*args) end |
#darkest(*args) ⇒ Object
73 74 75 |
# File 'lib/falu/palette.rb', line 73 def darkest(*args) swatches.sort_by { |swatch| swatch.color.hsl.lightness }.first(*args) end |
#dominant(*args) ⇒ Object
107 108 109 |
# File 'lib/falu/palette.rb', line 107 def dominant(*args) swatches.sort_by(&:count).reverse.first(*args) end |
#each(&block) ⇒ Object
30 31 32 |
# File 'lib/falu/palette.rb', line 30 def each(&block) swatches.each(&block) end |
#greens(*args) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/falu/palette.rb', line 87 def greens(*args) #swatches.sort_by { |swatch| swatch.color.rgb.green }.reverse.first(*args) swatches.sort do |a,b| agrn = a.color.rgb.green - (a.color.rgb.blue + a.color.rgb.red) bgrn = b.color.rgb.green - (b.color.rgb.blue + b.color.rgb.red) agrn <=> bgrn end.first(*args) end |
#lightest(*args) ⇒ Object
69 70 71 |
# File 'lib/falu/palette.rb', line 69 def lightest(*args) swatches.sort_by { |swatch| swatch.color.hsl.lightness }.reverse.first(*args) end |
#map(&block) ⇒ Object
34 35 36 |
# File 'lib/falu/palette.rb', line 34 def map(&block) swatches.map(&block) end |
#primary ⇒ Object
116 117 118 |
# File 'lib/falu/palette.rb', line 116 def primary @primary ||= dominant#(3).sort_by { |swtch| swtch.color.rgb.colors.sum }.first end |
#primary=(pri) ⇒ Object
111 112 113 114 |
# File 'lib/falu/palette.rb', line 111 def primary=(pri) # TODO check that the color exists in the palette @primary = pri end |
#reds(*args) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/falu/palette.rb', line 77 def reds(*args) #swatches.sort_by { |swatch| swatch.color.rgb.red }.reverse.first(*args) swatches.sort do |a,b| ared = a.color.rgb.red - (a.color.rgb.blue + a.color.rgb.green) bred = b.color.rgb.red - (b.color.rgb.blue + b.color.rgb.green) ared <=> bred end.first(*args) end |
#reverse! ⇒ Object
46 47 48 49 |
# File 'lib/falu/palette.rb', line 46 def reverse! swatches.reverse! self end |
#secondary ⇒ Object
125 126 127 128 129 |
# File 'lib/falu/palette.rb', line 125 def secondary @secondary ||= begin dominant(3).last(2).sort_by { |swtch| (swtch.color.rgb.colors.sum - primary.color.rgb.colors.sum).abs }.last end end |
#secondary=(sec) ⇒ Object
120 121 122 123 |
# File 'lib/falu/palette.rb', line 120 def secondary=(sec) # TODO check that the color exists in the palette @secondary = sec end |
#sort!(*args, &block) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/falu/palette.rb', line 51 def sort!(*args, &block) if args.empty? && !block_given? swatches.sort_by!(&:count).reverse! else swatches.sort!(*args, &block) end self end |
#sort_by!(*args, &block) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/falu/palette.rb', line 60 def sort_by!(*args, &block) if args.empty? && !block_given? swatches.sort_by!(&:count).reverse! else swatches.sort_by!(*args, &block) end self end |
#sum(*args, &block) ⇒ Object
38 39 40 |
# File 'lib/falu/palette.rb', line 38 def sum(*args, &block) swatches.sum(*args, &block) end |