Module: Autocad::Color

Defined in:
lib/autocad.rb

Constant Summary collapse

Red =

Standard AutoCAD color indices

1
Yellow =
2
Green =
3
Cyan =
4
Blue =
5
Magenta =
6
White =
7
Black =
0
Gray =

Additional common colors

8
LightGray =
9
DarkRed =
10
DarkGreen =
96
DarkBlue =
174
Orange =
30
Purple =
200
Brown =
35

Class Method Summary collapse

Class Method Details

.from_index(index) ⇒ Object

Convert an integer color index to a symbolic name if possible



65
66
67
68
69
70
# File 'lib/autocad.rb', line 65

def self.from_index(index)
  constants.each do |const_name|
    return underscore(const_name).to_sym if const_get(const_name) == index
  end
  index # Return the original index if no matching constant
end

.to_index(color) ⇒ Object

Convert a color symbol or name to its integer value

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/autocad.rb', line 42

def self.to_index(color)
  return color if color.is_a?(Integer)
  
  # Handle both camelCase and snake_case in symbols and strings
  color_str = color.to_s
  
  # Try direct match first (for exact constant names)
  constants.each do |const_name|
    return const_get(const_name) if const_name.to_s.downcase == color_str.downcase
  end
  
  # Try normalized version (convert snake_case to CamelCase)
  normalized = color_str.split('_').map(&:capitalize).join
  constants.each do |const_name|
    return const_get(const_name) if const_name.to_s.downcase == normalized.downcase
  end
  
  raise ArgumentError, "Unknown color: #{color}. Use a valid color name or integer index."
end

.underscore(camel_case) ⇒ Object

Helper method to convert to snake_case



75
76
77
78
79
# File 'lib/autocad.rb', line 75

def self.underscore(camel_case)
  camel_case.to_s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
            .gsub(/([a-z\d])([A-Z])/, '\1_\2')
            .downcase
end