Class: Chroma::Harmonies

Inherits:
Object
  • Object
show all
Defined in:
lib/chroma/harmonies.rb

Overview

Class to hold all palette methods.

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Harmonies


5
6
7
# File 'lib/chroma/harmonies.rb', line 5

def initialize(color)
  @color = color
end

Instance Method Details

#analogous(options = {}) ⇒ Array<Color>, Array<String>

Generate an analogous palette.

Examples:

'red'.paint.palette.analogous                        #=> [red, #ff0066, #ff0033, red, #ff3300, #ff6600]
'red'.paint.palette.analogous(as: :hex)              #=> ['#f00', '#f06', '#f03', '#f00', '#f30', '#f60']
'red'.paint.palette.analogous(size: 3)               #=> [red, #ff001a, #ff1a00]
'red'.paint.palette.analogous(size: 3, slice_by: 60) #=> [red, #ff000d, #ff0d00]

Options Hash (options):

  • :size (Symbol) — default: 6

    number of results to return

  • :slice_by (Symbol) — default: 30

    the angle in degrees to slice the hue circle per color

  • :as (Symbol) — default: nil

    optional format to output colors as strings


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chroma/harmonies.rb', line 79

def analogous(options = {})
  size = options[:size] || 6
  slices = options[:slice_by] || 30

  hsl = @color.hsl
  part = 360 / slices
  hsl.h = ((hsl.h - (part * size >> 1)) + 720) % 360

  palette = (size - 1).times.reduce([@color]) do |arr, n|
    hsl.h = (hsl.h + part) % 360
    arr << Color.new(hsl, @color.format)
  end

  with_reformat(palette, options[:as])
end

#complement(options = {}) ⇒ Array<Color>, Array<String>

Generate a complement palette.

Examples:

'red'.paint.palette.complement            #=> [red, cyan]
'red'.paint.palette.complement(as: :name) #=> ['red', 'cyan']
'red'.paint.palette.complement(as: :hex)  #=> ['#ff0000', '#00ffff']

Options Hash (options):

  • :as (Symbol) — default: nil

    optional format to output colors as strings


19
20
21
# File 'lib/chroma/harmonies.rb', line 19

def complement(options = {})
  with_reformat([@color, @color.complement], options[:as])
end

#monochromatic(options = {}) ⇒ Array<Color>, Array<String>

Generate a monochromatic palette.

Examples:

'red'.paint.palette.monochromatic           #=> [red, #2a0000, #550000, maroon, #aa0000, #d40000]
'red'.paint.palette.monochromatic(as: :hex) #=> ['#ff0000', '#2a0000', '#550000', '#800000', '#aa0000', '#d40000']
'red'.paint.palette.monochromatic(size: 3)  #=> [red, #550000, #aa0000]

Options Hash (options):

  • :size (Symbol) — default: 6

    number of results to return

  • :as (Symbol) — default: nil

    optional format to output colors as strings


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chroma/harmonies.rb', line 106

def monochromatic(options = {})
  size = options[:size] || 6

  h, s, v = @color.hsv
  modification = 1.0 / size

  palette = size.times.map do
    Color.new(ColorModes::Hsv.new(h, s, v), @color.format).tap do
      v = (v + modification) % 1
    end
  end

  with_reformat(palette, options[:as])
end

#split_complement(options = {}) ⇒ Array<Color>, Array<String>

Generate a split complement palette.

Examples:

'red'.paint.palette.split_complement            #=> [red, #ccff00, #0066ff]
'red'.paint.palette.split_complement(as: :name) #=> ['red', '#ccff00', '#0066ff']
'red'.paint.palette.split_complement(as: :hex)  #=> ['#ff0000', '#ccff00', '#0066ff']

Options Hash (options):

  • :as (Symbol) — default: nil

    optional format to output colors as strings


61
62
63
# File 'lib/chroma/harmonies.rb', line 61

def split_complement(options = {})
  hsl_map([0, 72, 216], options)
end

#tetrad(options = {}) ⇒ Array<Color>, Array<String>

Generate a tetrad palette.

Examples:

'red'.paint.palette.tetrad            #=> [red, #80ff00, cyan, #7f00ff]
'red'.paint.palette.tetrad(as: :name) #=> ['red', '#80ff00', 'cyan', '#7f00ff']
'red'.paint.palette.tetrad(as: :hex)  #=> ['#ff0000', '#80ff00', '#00ffff', '#7f00ff']

Options Hash (options):

  • :as (Symbol) — default: nil

    optional format to output colors as strings


47
48
49
# File 'lib/chroma/harmonies.rb', line 47

def tetrad(options = {})
  hsl_map([0, 90, 180, 270], options)
end

#triad(options = {}) ⇒ Array<Color>, Array<String>

Generate a triad palette.

Examples:

'red'.paint.palette.triad            #=> [red, lime, blue]
'red'.paint.palette.triad(as: :name) #=> ['red', 'lime', 'blue']
'red'.paint.palette.triad(as: :hex)  #=> ['#ff0000', '#00ff00', '#0000ff']

Options Hash (options):

  • :as (Symbol) — default: nil

    optional format to output colors as strings


33
34
35
# File 'lib/chroma/harmonies.rb', line 33

def triad(options = {})
  hsl_map([0, 120, 240], options)
end