Class: Procreate::Swatches::Wrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • name (String)

    Name of the swatches palette

  • colors (Array<Chroma::Color, String>)

    Colors array. Each color will be converted to a Chroma::Color instance.



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.

Parameters:

  • format (String, Symbol) (defaults to: nil)

    Format to return the colors array in.

Returns:

  • (Array<Chroma::Color>)

    Return an array of Chroma::Color when no format is provided. This is the default option.

  • (Array<String>)

    Return an array of Strings, in the specified 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

#nameString

Returns Name of the swatches palette.

Returns:

  • (String)

    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.

Parameters:

  • color (Chroma::Color, String)

    Color in a supported format.

Returns:

  • (Array<Chroma::Color>)

    The current colors available in the wrapper.



42
43
44
# File 'lib/procreate/swatches/wrapper.rb', line 42

def <<(color)
  add_color(color)
end

#==(other) ⇒ Boolean

TODO:

Check if the colors array is the same (regardless of order)

Wrapper file comparison. It check for the same class, name and colors array

Parameters:

Returns:

  • (Boolean)

    The wrappers are equal



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_formatsArray<Symbol>

Returns the list of available color formats a color can be retrieved into.

Returns:

  • (Array<Symbol>)

    The list of available formats a color can be retrived into.



66
67
68
# File 'lib/procreate/swatches/wrapper.rb', line 66

def available_color_formats
  AVAILABLE_COLOR_FORMATS
end

#popArray<Chroma::Color>

Remove the last color from the colors array.

Returns:

  • (Array<Chroma::Color>)

    The current colors available in the wrapper.



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

Parameters:

Returns:

  • (String)

    swatches_path Path of the exported .swatches file



119
120
121
# File 'lib/procreate/swatches/wrapper.rb', line 119

def to_file(options = {})
  Exporter.call(self, options)
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.

Returns:

  • (String)

    Wrapper content, in JSON format.



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