Class: Procreate::Swatches::Wrapper
- Inherits:
-
Object
- Object
- Procreate::Swatches::Wrapper
- Includes:
- ColorsHelper
- Defined in:
- lib/procreate/swatches/wrapper.rb
Overview
Wrapper class for the name and colors of a .swatches
file.
Constant Summary collapse
- DEFAULT_SWATCHES_NAME =
The default name, used in case a custom name is not provided
'My beautiful palette'
- AVAILABLE_COLOR_FORMATS =
Available formats to return the colors as. These types are supported by Chroma::Color
%i[hsv hsl hex hex8 rgb name].freeze
Constants included from ColorsHelper
ColorsHelper::SELECTED_KEYS, ColorsHelper::SWATCHES_ALPHA, ColorsHelper::SWATCHES_COLOR_SPACE, ColorsHelper::SWATCHES_MAX_SIZE
Instance Attribute Summary collapse
-
#colors(format: nil) ⇒ Array<Chroma::Color>, Array<String>
Attribute reader for colors.
-
#name ⇒ String
Name of the swatches palette.
Instance Method Summary collapse
-
#<<(color) ⇒ Array<Chroma::Color>
(also: #push)
Add a new color to the colors array.
-
#==(other) ⇒ Boolean
Wrapper file comparison.
-
#available_color_formats ⇒ Array<Symbol>
Returns the list of available color formats a color can be retrieved into.
-
#initialize(name, colors) ⇒ Wrapper
constructor
Initialize a new instance of Wrapper.
-
#pop ⇒ Array<Chroma::Color>
Remove the last color from the colors array.
-
#to_file(options = {}) ⇒ String
(also: #export)
Export the wrapper to a
.swatches
file. -
#to_json(*_args) ⇒ String
(also: #to_swatches, #to_procreate)
Convert the wrapper to the JSON format needed to create a
.swatches
file.
Methods included from ColorsHelper
#prepare_color_for_push, #to_chroma_hsv, #to_color_json, #to_swatches_json
Constructor Details
#initialize(name, colors) ⇒ Wrapper
Initialize a new instance of Procreate::Swatches::Wrapper
27 28 29 30 31 32 |
# File 'lib/procreate/swatches/wrapper.rb', line 27 def initialize(name, colors) @name = name.present? ? name : DEFAULT_SWATCHES_NAME @colors ||= [] Array(colors).each { |color| add_color(color) } end |
Instance Attribute Details
#colors(format: nil) ⇒ Array<Chroma::Color>, Array<String>
Attribute reader for colors. It supports returning the colors in different formats. If not format is provided, an array of Chroma::Colors is returned. If an invalid format is provided, it will fallback to the default format.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/procreate/swatches/wrapper.rb', line 81 def colors(format: nil) format = format.to_sym unless format.nil? format = nil unless format.in?(AVAILABLE_COLOR_FORMATS) return @colors if format.nil? @colors.map do |color| color.send("to_#{format}") end end |
#name ⇒ String
Returns Name of the swatches palette.
13 14 15 |
# File 'lib/procreate/swatches/wrapper.rb', line 13 def name @name end |
Instance Method Details
#<<(color) ⇒ Array<Chroma::Color> Also known as: push
Add a new color to the colors array. It will be converted to a Chroma::Color instance.
42 43 44 |
# File 'lib/procreate/swatches/wrapper.rb', line 42 def <<(color) add_color(color) end |
#==(other) ⇒ Boolean
Check if the colors array is the same (regardless of order)
Wrapper file comparison. It check for the same class, name and colors array
134 135 136 |
# File 'lib/procreate/swatches/wrapper.rb', line 134 def ==(other) other.is_a?(self.class) && name == other.name && colors == other.colors end |
#available_color_formats ⇒ Array<Symbol>
Returns the list of available color formats a color can be retrieved into.
66 67 68 |
# File 'lib/procreate/swatches/wrapper.rb', line 66 def available_color_formats AVAILABLE_COLOR_FORMATS end |
#pop ⇒ Array<Chroma::Color>
Remove the last color from the colors array.
53 54 55 |
# File 'lib/procreate/swatches/wrapper.rb', line 53 def pop @colors.pop end |
#to_file(options = {}) ⇒ String Also known as: export
Export the wrapper to a .swatches
file
119 120 121 |
# File 'lib/procreate/swatches/wrapper.rb', line 119 def to_file( = {}) Exporter.call(self, ) end |
#to_json(*_args) ⇒ String Also known as: to_swatches, to_procreate
Convert the wrapper to the JSON format needed to create a .swatches
file. A maximum of 30 (ColorsHelper::SWATCHES_MAX_SIZE) colors is returned. This is a limitation of Procreate.
100 101 102 103 104 105 106 107 |
# File 'lib/procreate/swatches/wrapper.rb', line 100 def to_json(*_args) [ { name: @name, swatches: colors_to_json } ].to_json end |