Module: Chroma::Helpers::Bounders

Included in:
Color, Converters::Base, RgbGenerator::Base
Defined in:
lib/chroma/helpers/bounders.rb

Instance Method Summary collapse

Instance Method Details

#bound01(n, max) ⇒ Float

Bounds a value n that is from 0 to max to 0 to 1.

Parameters:

  • n (Numeric, String)
  • max (Fixnum)

Returns:

  • (Float)


9
10
11
12
13
14
15
16
17
# File 'lib/chroma/helpers/bounders.rb', line 9

def bound01(n, max)
  is_percent = n.to_s.include? '%'
  n = [max, [0, n.to_f].max].min
  n = (n * max).to_i / 100.0 if is_percent

  return 1 if (n - max).abs < 0.000001

  (n % max) / max.to_f
end

#bound_alpha(a) ⇒ Numeric

Ensure alpha value a is between 0 and 1.

Parameters:

  • a (Numeric, String)

    alpha value

Returns:

  • (Numeric)


23
24
25
26
27
# File 'lib/chroma/helpers/bounders.rb', line 23

def bound_alpha(a)
  a = a.to_f
  a = 1 if a < 0 || a > 1
  a
end

#clamp01(n) ⇒ Numeric

Ensures a number between 0 and 1. Returns n if it is between 0 and 1.

Parameters:

  • n (Numeric)

Returns:

  • (Numeric)


34
35
36
# File 'lib/chroma/helpers/bounders.rb', line 34

def clamp01(n)
  [1, [0, n].max].min
end

#to_percentage(n) ⇒ String, Float

Converts n to a percentage type value.

Parameters:

Returns:



42
43
44
45
46
# File 'lib/chroma/helpers/bounders.rb', line 42

def to_percentage(n)
  n = n.to_f
  n = "#{n * 100}%" if n <= 1
  n
end