Module: ColorLib

Defined in:
lib/color_lib.rb,
lib/color_lib/version.rb,
lib/color_lib/grayscale.rb

Defined Under Namespace

Modules: CSS, Palette Classes: CMYK, GrayScale, HSL, Pantone, RGB, YIQ

Constant Summary collapse

COLOR_VERSION =
VERSION
COLOR_EPSILON =

The maximum “resolution” for colour math; if any value is less than or equal to this value, it is treated as zero.

1e-5
COLOR_TOLERANCE =

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.

1e-4
VERSION =
"1.4.9"
GreyScale =

A synonym for ColorLib::GrayScale.

GrayScale

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/color_lib.rb', line 87

def const_missing(name) #:nodoc:
  case name
  when "VERSION", :VERSION, "COLOR_TOOLS_VERSION", :COLOR_TOOLS_VERSION
    warn "ColorLib::#{name} has been deprecated. Use ColorLib::COLOR_VERSION instead."
    ColorLib::COLOR_VERSION
  else
    if ColorLib::RGB.const_defined?(name)
      warn "ColorLib::#{name} has been deprecated. Use ColorLib::RGB::#{name} instead."
      ColorLib::RGB.const_get(name)
    else
      super
    end
  end
end

.near_one?(value) ⇒ Boolean

Returns true if the value is within COLOR_EPSILON of one.

Returns:

  • (Boolean)


38
39
40
# File 'lib/color_lib.rb', line 38

def near_one?(value)
  near_zero?(value - 1.0)
end

.near_one_or_more?(value) ⇒ Boolean

Returns true if the value is within COLOR_EPSILON of one or more than one.

Returns:

  • (Boolean)


44
45
46
# File 'lib/color_lib.rb', line 44

def near_one_or_more?(value)
  (value > 1.0 or near_one?(value))
end

.near_zero?(value) ⇒ Boolean

Returns true if the value is less than COLOR_EPSILON.

Returns:

  • (Boolean)


27
28
29
# File 'lib/color_lib.rb', line 27

def near_zero?(value)
  (value.abs <= COLOR_EPSILON)
end

.near_zero_or_less?(value) ⇒ Boolean

Returns true if the value is within COLOR_EPSILON of zero or less than zero.

Returns:

  • (Boolean)


33
34
35
# File 'lib/color_lib.rb', line 33

def near_zero_or_less?(value)
  (value < 0.0 or near_zero?(value))
end

.new(values, mode = :rgb) ⇒ Object

Provides a thin veneer over the ColorLib module to make it seem like this is ColorLib 0.1.0 (a class) and not ColorLib 1.4.1 (a module). This “constructor” will be removed in the future.

mode = :hsl

values must be an array of [ hue deg, sat %, lum % ]. A ColorLib::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 ColorLib::RGB object will be created.

mode = :cmyk

values must be an array of [ cyan %, magenta %, yellow %, black % ]. A ColorLib::CMYK object will be created.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/color_lib.rb', line 113

def new(values, mode = :rgb)
  warn "ColorLib.new has been deprecated. Use ColorLib::#{mode.to_s.upcase}.new instead."
  color = case mode
          when :hsl
            ColorLib::HSL.new(*values)
          when :rgb
            values = [values].flatten
            if values.size == 1
              ColorLib::RGB.from_html(*values)
            else
              ColorLib::RGB.new(*values)
            end
          when :cmyk
            ColorLib::CMYK.new(*values)
          end
  color.to_hsl
end

.normalize(value) ⇒ Object Also known as: normalize_fractional

Normalizes the value to the range (0.0) .. (1.0).



49
50
51
52
53
54
55
56
57
# File 'lib/color_lib.rb', line 49

def normalize(value)
  if near_zero_or_less? value
    0.0
  elsif near_one_or_more? value
    1.0
  else
    value
  end
end

.normalize_byte(value) ⇒ Object Also known as: normalize_8bit

Normalize the value to the range (0) .. (255).



74
75
76
# File 'lib/color_lib.rb', line 74

def normalize_byte(value)
  normalize_to_range(value, 0..255).to_i
end

.normalize_to_range(value, range) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/color_lib.rb', line 61

def normalize_to_range(value, range)
  range = (range.end..range.begin) if (range.end < range.begin)

  if value <= range.begin
    range.begin
  elsif value >= range.end
    range.end
  else
    value
  end
end

.normalize_word(value) ⇒ Object Also known as: normalize_16bit

Normalize the value to the range (0) .. (65535).



81
82
83
# File 'lib/color_lib.rb', line 81

def normalize_word(value)
  normalize_to_range(value, 0..65535).to_i
end