Module: Procreate::Swatches::ColorsHelper

Included in:
Parser, Wrapper
Defined in:
lib/procreate/swatches/colors_helper.rb

Overview

Helper module to interact with colors

Constant Summary collapse

SWATCHES_MAX_SIZE =

.swatches files can hold a maximum of 30 colors.

30
SWATCHES_ALPHA =

Procreate .swatches don’t consider the opacity, therefore a default value of 1 is used.

1
SWATCHES_COLOR_SPACE =
0
SELECTED_KEYS =
%w[hue saturation brightness].freeze

Instance Method Summary collapse

Instance Method Details

#prepare_color_for_push(color) ⇒ Chroma::Color?

Checks a “color” before adding it to a Wrapper instance’s colors array.

Parameters:

Returns:

  • (Chroma::Color)

    Return a Chroma::Color if the color is already an instance of Chroma::Color, or the string can be converted to one.

  • (nil)

    Returns nil when Chroma::Color can’t build a color object from the provided string



72
73
74
75
76
77
78
79
80
# File 'lib/procreate/swatches/colors_helper.rb', line 72

def prepare_color_for_push(color)
  return color if color.is_a?(Chroma::Color)

  begin
    Chroma::Color.new(color.dup)
  rescue Chroma::Errors::UnrecognizedColor
    nil
  end
end

#to_chroma_hsv(color) ⇒ String

Generates a string formatted to initialize a Chroma::Color from HSV values

Parameters:

  • color (Hash{hue => Float, saturation => Float, brightness => Float})

    Color as hash (from parsed .swatches file)

Returns:

  • (String)

    string String formatted to initialize a Chroma::Color from HSV values



57
58
59
60
61
62
# File 'lib/procreate/swatches/colors_helper.rb', line 57

def to_chroma_hsv(color)
  hue, saturation, brightness = *color.values_at(*SELECTED_KEYS)
  hue, saturation, brightness = *[hue * 360, saturation * 100, brightness * 100].map(&:round)

  +"hsv(#{hue}, #{saturation}%, #{brightness}%)"
end

#to_color_json(hue, saturation, brightness) ⇒ Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer}

Transforms the HSV (HSB) parameters into a hash, to be exported to the .swatches file.

Parameters:

  • hue (Float, Integer)

    Hue

  • saturation (Float, Integer)

    Saturation

  • brightness (Float, Integer)

    Brightness/Value

Returns:

  • (Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer})


40
41
42
43
44
45
46
47
48
# File 'lib/procreate/swatches/colors_helper.rb', line 40

def to_color_json(hue, saturation, brightness)
  {
    hue: hue,
    saturation: saturation,
    brightness: brightness,
    alpha: SWATCHES_ALPHA,
    colorSpace: SWATCHES_COLOR_SPACE
  }
end

#to_swatches_json(color) ⇒ Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer}

Transforms the Chroma::Color color be exported to the .swatches file.

Parameters:

  • color (Chroma::Color)

    Color

Returns:

  • (Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer})


20
21
22
23
24
25
26
27
28
# File 'lib/procreate/swatches/colors_helper.rb', line 20

def to_swatches_json(color)
  hsv_color = color.hsv

  hue = hsv_color.h / 360
  saturation = hsv_color.s
  brightness = hsv_color.v

  to_color_json(hue, saturation, brightness)
end