Class: Color::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/color/maker.rb,
lib/color/maker/support.rb,
lib/color/maker/version.rb

Overview

Create random pleasing colors as well as color schemes based on a given color.

Defined Under Namespace

Modules: Support

Constant Summary collapse

PHI =
0.618033988749895
VERSION =
"0.0.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Maker

Returns a new instance of Maker

Options Hash (options):

  • :hue (Symbol) — default: nil

    By setting the hue, you determine the color. (0..360)

  • :saturation (Symbol) — default: nil

    By setting the saturation, you determine the distance from gray. (0..1.0)

  • :value (Symbol) — default: nil

    By setting the value, you determine the balance between black and white. (0..1.0)

  • :base_color (Symbol) — default: nil

    Setting a base_color (e.g. “pink”) will create a random color within the HSV range of the chosen color. Will recognize any of the 146 standard HTML colors, it has a very good memory.

  • :greyscale (Symbol) — default: false

    for the brits - Setting either greyscale or grayscale (but we all know which one is correct) to true will cause all of the colors you generate to be within the grey or gray range. This is effectively the same as setting your saturation to 0.

  • :grayscale (Symbol) — default: false

    for the yanks - Setting either greyscale or grayscale (but we all know which one is correct) to true will cause all of the colors you generate to be within the grey or gray range. This is effectively the same as setting your saturation to 0.

  • :golden (Symbol) — default: true

    Setting golden to true randomizes your hue (overrides hue setting) and makes you a spectacular color based on the golden ratio. It's so good, it's the default. Make sure to turn it off if you want to have more control over your generated colors.

  • :random (Symbol) — default: false

    It will completely randomize the hue, saturation, and value of the colors it makes.

  • :count (Symbol) — default: 1

    Setting count to higher than 1 will return an array full of the colors. If you set it to 1, you'll just get the one color! It makes a sort of sense if you think about it.

  • :red (Symbol) — default: nil

    red value for the color to start with (0..255)

  • :green (Symbol) — default: nil

    green value for the color to start with (0..255)

  • :blue (Symbol) — default: nil

    blue value for the color to start with (0..255)

  • :lightness (Symbol) — default: nil

    the luminosity of the colour (0..1.0)


102
103
104
105
106
# File 'lib/color/maker.rb', line 102

def initialize(options = {})
  options = normalize_options(options)
  @options = self.class.defaults.merge(options)
  @generator = Random.new(options[:seed]) rescue Random.new
end

Class Attribute Details

.defaultsObject (readonly)


65
66
67
# File 'lib/color/maker.rb', line 65

def defaults
  @defaults
end

Instance Attribute Details

#generatorObject

Value generator, Random class instance


7
8
9
# File 'lib/color/maker.rb', line 7

def generator
  @generator
end

Class Method Details

.by_name(name) ⇒ Object

Return a Color::RGB object

Examples:

Color::Maker.by_name(:aquamarine) #=> Color::RGB
Color::Maker.by_name(:blue) #=> Color::RGB
Color::Maker.by_name(:aliceblue) #=> Color::RGB

94
95
96
# File 'lib/color/maker.rb', line 94

def by_name(name)
  self.colors[name.downcase.to_sym]
end

.colorsHash

Memorized colors

Examples:

Color::Maker.colors(:aquamarine) #=> 7FFFD4

72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/color/maker.rb', line 72

def colors
  return @colors if @colors
  @hex_colors = ::YAML.load_file(@colors_path)
  @hex_colors.symbolize_keys!
  @colors = Hash.new do |h, k|
    if @hex_colors[k]
      h[k] = Color::RGB.by_hex(@hex_colors[k])
    else
      h[k] = Color::RGB.by_name(k)
    end
  end
end

Instance Method Details

#make(options = {}) ⇒ Object

Generates a new color

Options Hash (options):

  • :hue (Symbol) — default: nil

    By setting the hue, you determine the color. (0..360)

  • :saturation (Symbol) — default: nil

    By setting the saturation, you determine the distance from gray. (0..1.0)

  • :value (Symbol) — default: nil

    By setting the value, you determine the balance between black and white. (0..1.0)

  • :base_color (Symbol) — default: nil

    Setting a base_color (e.g. “pink”) will create a random color within the HSV range of the chosen color. Will recognize any of the 146 standard HTML colors, it has a very good memory.

  • :greyscale (Symbol) — default: false

    for the brits - Setting either greyscale or grayscale (but we all know which one is correct) to true will cause all of the colors you generate to be within the grey or gray range. This is effectively the same as setting your saturation to 0.

  • :grayscale (Symbol) — default: false

    for the yanks - Setting either greyscale or grayscale (but we all know which one is correct) to true will cause all of the colors you generate to be within the grey or gray range. This is effectively the same as setting your saturation to 0.

  • :golden (Symbol) — default: true

    Setting golden to true randomizes your hue (overrides hue setting) and makes you a spectacular color based on the golden ratio. It's so good, it's the default. Make sure to turn it off if you want to have more control over your generated colors.

  • :random (Symbol) — default: false

    It will completely randomize the hue, saturation, and value of the colors it makes.

  • :count (Symbol) — default: 1

    Setting count to higher than 1 will return an array full of the colors. If you set it to 1, you'll just get the one color! It makes a sort of sense if you think about it.

  • :red (Symbol) — default: nil

    red value for the color to start with (0..255)

  • :green (Symbol) — default: nil

    green value for the color to start with (0..255)

  • :blue (Symbol) — default: nil

    blue value for the color to start with (0..255)

  • :lightness (Symbol) — default: nil

    the luminosity of the colour (0..1.0)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/color/maker.rb', line 114

def make(options = {})
  generator = self.generator
  generator = Random.new(options[:seed]) if options[:seed]

  options = normalize_options(options)
  options = self.class.defaults.merge(@options.merge(options))

  base_color = self.class.by_name(options[:base_color]) if options[:base_color]
  colors = []
  options[:count].times do |i|
    if base_color
      base_color = base_color.to_hsl
      base_color = Support::hsl_to_hsv(h: base_color.hue, s: base_color.s, l: base_color.l)

      hue = generator.rand((base_color[:h] - 5)..(base_color[:h] + 5))
      saturation = generator.rand(0.4..0.85)
      value = generator.rand(0.4..0.85)
      colors << [hue, saturation, value].to_color(:hsv)
    else
      hue = make_hue(options, generator)
      saturation = make_saturation(options, generator)
      value = make_value(options, generator)

      colors << [hue, saturation, value].to_color(:hsv)
    end
  end

  colors = colors[0] if colors.size == 1
  colors
end