Class: ColorGenerator
- Inherits:
-
Object
- Object
- ColorGenerator
- 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
-
#hue ⇒ Object
readonly
Returns the value of attribute hue.
-
#lightness ⇒ Object
readonly
Returns the value of attribute lightness.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#saturation ⇒ Object
readonly
Returns the value of attribute saturation.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.rgb_from_hsl(h, s, l) ⇒ Array
Converts a color from HSL to RGB.
-
.rgb_from_hsv(h, s, v) ⇒ Array
Converts a color from HSV to RGB.
Instance Method Summary collapse
-
#create_hex ⇒ String
(also: #create)
Formats a random color as an RGB hex triplet.
-
#create_rgb ⇒ Array
Generates a random color as an RGB decimal triplet.
-
#hsl? ⇒ Boolean
Whether the color representation is HSL.
-
#hsv? ⇒ Boolean
Whether the color representation is HSV.
-
#initialize(opts = {}) ⇒ ColorGenerator
constructor
Initializes a color generator.
Constructor Details
#initialize(opts = {}) ⇒ ColorGenerator
Initializes a color generator.
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
#hue ⇒ Object (readonly)
Returns the value of attribute hue.
4 5 6 |
# File 'lib/color-generator.rb', line 4 def hue @hue end |
#lightness ⇒ Object (readonly)
Returns the value of attribute lightness.
4 5 6 |
# File 'lib/color-generator.rb', line 4 def lightness @lightness end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
4 5 6 |
# File 'lib/color-generator.rb', line 4 def mode @mode end |
#saturation ⇒ Object (readonly)
Returns the value of attribute saturation.
4 5 6 |
# File 'lib/color-generator.rb', line 4 def saturation @saturation end |
#value ⇒ Object (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.
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.
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_hex ⇒ String Also known as: create
Formats a random color as an RGB hex triplet.
36 37 38 |
# File 'lib/color-generator.rb', line 36 def create_hex '%02x%02x%02x' % create_rgb end |
#create_rgb ⇒ Array
Generates a random color as 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.
56 57 58 |
# File 'lib/color-generator.rb', line 56 def hsl? mode == :HSL end |
#hsv? ⇒ Boolean
Returns whether the color representation is HSV.
61 62 63 |
# File 'lib/color-generator.rb', line 61 def hsv? mode == :HSV end |