Class: ColorGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/color-generator.rb,
lib/color-generator/version.rb

Constant Summary collapse

GOLDEN_RATIO_CONJUGATE =
0.618033988749895
VERSION =
"0.0.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ColorGenerator

Initializes a color generator.

Parameters:

  • opts (Hash) (defaults to: {})

    optional arguments

Options Hash (opts):

  • :saturation (Float, Integer)

    saturation in the interval [0, 1]

  • :lightness (Float, Integer)

    lightness in the interval [0, 1], sets the color representation to HSL

  • :value (Float, Integer)

    value in the interval [0, 1], sets the color representation to HSV

  • :seed (Integer)

    seed for the pseudorandom number generator

  • :hue (Float, Integer)

    hue to use as the start in the interval [0, 1]



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/color-generator.rb', line 16

def initialize(opts = {})
  if opts.key?(:hue)
    @hue = opts[:hue]
  else
    srand(opts[:seed]) if opts.key?(:seed)
    @hue = rand
  end
  @saturation = opts[:saturation].to_f
  if opts.has_key? :lightness
    @mode = :HSL
    @lightness = opts[:lightness].to_f
  else
    @mode = :HSV
    @value = opts[:value].to_f
  end
end

Instance Attribute Details

#hueObject (readonly)

Returns the value of attribute hue.



4
5
6
# File 'lib/color-generator.rb', line 4

def hue
  @hue
end

#lightnessObject (readonly)

Returns the value of attribute lightness.



4
5
6
# File 'lib/color-generator.rb', line 4

def lightness
  @lightness
end

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/color-generator.rb', line 4

def mode
  @mode
end

#saturationObject (readonly)

Returns the value of attribute saturation.



4
5
6
# File 'lib/color-generator.rb', line 4

def saturation
  @saturation
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/color-generator.rb', line 4

def value
  @value
end

Class Method Details

.rgb_from_hsl(h, s, l) ⇒ Array

Converts a color from HSL to RGB.

Parameters:

  • h (Float)

    hue in the interval [0, 1]

  • s (Float)

    saturation in the interval [0, 1]

  • l (Float)

    lightness in the interval [0, 1]

Returns:

  • (Array)

    an RGB decimal triplet



71
72
73
74
75
# File 'lib/color-generator.rb', line 71

def self.rgb_from_hsl(h, s, l)
  c = (1 - (2 * l - 1).abs) * s
  m = l - 0.5 * c
  rgb_from_hsl_or_hsv h, c, m
end

.rgb_from_hsv(h, s, v) ⇒ Array

Converts a color from HSV to RGB.

Parameters:

  • h (Float)

    hue in the interval [0, 1]

  • s (Float)

    saturation in the interval [0, 1]

  • v (Float)

    value in the interval [0, 1]

Returns:

  • (Array)

    an RGB decimal triplet



83
84
85
86
87
# File 'lib/color-generator.rb', line 83

def self.rgb_from_hsv(h, s, v)
  c = v * s
  m = v - c
  rgb_from_hsl_or_hsv h, c, m
end

Instance Method Details

#create_hexString Also known as: create

Formats a random color as an RGB hex triplet.

Returns:

  • (String)

    an RGB hex triplet



36
37
38
# File 'lib/color-generator.rb', line 36

def create_hex
  '%02x%02x%02x' % create_rgb
end

#create_rgbArray

Generates a random color as an RGB decimal triplet.

Returns:

  • (Array)

    an RGB decimal triplet



46
47
48
49
50
51
52
53
# File 'lib/color-generator.rb', line 46

def create_rgb
  @hue = (hue + GOLDEN_RATIO_CONJUGATE) % 1
  if hsl?
    self.class.rgb_from_hsl hue, saturation, lightness
  else
    self.class.rgb_from_hsv hue, saturation, value
  end
end

#hsl?Boolean

Returns whether the color representation is HSL.

Returns:

  • (Boolean)

    whether the color representation is HSL



56
57
58
# File 'lib/color-generator.rb', line 56

def hsl?
  mode == :HSL
end

#hsv?Boolean

Returns whether the color representation is HSV.

Returns:

  • (Boolean)

    whether the color representation is HSV



61
62
63
# File 'lib/color-generator.rb', line 61

def hsv?
  mode == :HSV
end