| Module | Color |
| In: |
lib/color.rb
lib/color/grayscale.rb |
| COLOR_VERSION | = | '1.4.0' | ||
| COLOR_EPSILON | = | 1e-5 | The maximum "resolution" for colour math; if any value is less than or equal to this value, it is treated as zero. | |
| COLOR_TOLERANCE | = | 1e-4 | The tolerance for comparing the components of two colours. In general, colours are considered equal if all of their components are within this tolerance value of each other. | |
| GreyScale | = | GrayScale | A synonym for Color::GrayScale. |
| normalize | -> | normalize_fractional |
| normalize_byte | -> | normalize_8bit |
| normalize_word | -> | normalize_16bit |
Returns true if the value is within COLOR_EPSILON of one.
# File lib/color.rb, line 48
48: def near_one?(value)
49: near_zero?(value - 1.0)
50: end
Returns true if the value is within COLOR_EPSILON of one or more than one.
# File lib/color.rb, line 54
54: def near_one_or_more?(value)
55: (value > 1.0 or near_one?(value))
56: end
Returns true if the value is less than COLOR_EPSILON.
# File lib/color.rb, line 37
37: def near_zero?(value)
38: (value.abs <= COLOR_EPSILON)
39: end
Returns true if the value is within COLOR_EPSILON of zero or less than zero.
# File lib/color.rb, line 43
43: def near_zero_or_less?(value)
44: (value < 0.0 or near_zero?(value))
45: end
Provides a thin veneer over the Color module to make it seem like this is Color 0.1.0 (a class) and not Color 1.4.0 (a module). This "constructor" will be removed in the future.
| mode = :hsl: | values must be an array of [ hue deg, sat %, lum % ]. A Color::HSL object will be created. |
| mode = :rgb: | values will either be an HTML-style colour string or an array of [ red, green, blue ] (range 0 .. 255). A Color::RGB object will be created. |
| mode = :cmyk: | values must be an array of [ cyan %, magenta %, yellow %, black % ]. A Color::CMYK object will be created. |
# File lib/color.rb, line 130
130: def self.new(values, mode = :rgb)
131: warn "Color.new has been deprecated. Use Color::#{mode.to_s.upcase}.new instead."
132: color = case mode
133: when :hsl
134: Color::HSL.new(*values)
135: when :rgb
136: values = [ values ].flatten
137: if values.size == 1
138: Color::RGB.from_html(*values)
139: else
140: Color::RGB.new(*values)
141: end
142: when :cmyk
143: Color::CMYK.new(*values)
144: end
145: color.to_hsl
146: end
Normalizes the value to the range (0.0) .. (1.0).
# File lib/color.rb, line 59
59: def normalize(value)
60: if near_zero_or_less? value
61: 0.0
62: elsif near_one_or_more? value
63: 1.0
64: else
65: value
66: end
67: end
Normalize the value to the range (0) .. (255).
# File lib/color.rb, line 83
83: def normalize_byte(value)
84: normalize_to_range(value, 0..255).to_i
85: end
# File lib/color.rb, line 70
70: def normalize_to_range(value, range)
71: range = (range.end..range.begin) if (range.end < range.begin)
72:
73: if value <= range.begin
74: range.begin
75: elsif value >= range.end
76: range.end
77: else
78: value
79: end
80: end