Class: Unmagic::Color::Units::Degrees
- Inherits:
-
Object
- Object
- Unmagic::Color::Units::Degrees
- Includes:
- Comparable
- Defined in:
- lib/unmagic/color/units/degrees.rb,
lib/unmagic/color/units/direction.rb
Overview
Represents an angle in degrees (0-360°).
Supports numeric values, degree strings, and named direction keywords. Values are automatically wrapped to the 0-360 range.
## Supported Formats
-
Numeric: ‘225`, `45.5`
-
Degree strings: ‘“225deg”`, `“45.5deg”`, `“-45deg”`, `“225°”`, `“45.5°”`
-
Named directions: ‘“top”`, `“bottom left”`, `“north”`, `“southwest”`, etc.
## Named Direction Keywords
-
Cardinal: ‘“top”` (0°), `“right”` (90°), `“bottom”` (180°), `“left”` (270°)
-
Diagonal: ‘“top right”` (45°), `“bottom right”` (135°), `“bottom left”` (225°), `“top left”` (315°)
-
Aliases: ‘“north”`, `“south”`, `“east”`, `“west”`, `“northeast”`, etc.
Defined Under Namespace
Classes: Direction, ParseError
Constant Summary collapse
- TOP =
Predefined degree constants
new(value: 0, name: "top", aliases: ["north"]).freeze
- RIGHT =
Right direction (90°, east)
new(value: 90, name: "right", aliases: ["east"]).freeze
- BOTTOM =
Bottom direction (180°, south)
new(value: 180, name: "bottom", aliases: ["south"]).freeze
- LEFT =
Left direction (270°, west)
new(value: 270, name: "left", aliases: ["west"]).freeze
- TOP_RIGHT =
Top-right diagonal direction (45°, northeast)
new(value: 45, name: "top right", aliases: ["topright", "northeast", "north east"]).freeze
- BOTTOM_RIGHT =
Bottom-right diagonal direction (135°, southeast)
new(value: 135, name: "bottom right", aliases: ["bottomright", "southeast", "south east"]).freeze
- BOTTOM_LEFT =
Bottom-left diagonal direction (225°, southwest)
new(value: 225, name: "bottom left", aliases: ["bottomleft", "southwest", "south west"]).freeze
- TOP_LEFT =
Top-left diagonal direction (315°, northwest)
new(value: 315, name: "top left", aliases: ["topleft", "northwest", "north west"]).freeze
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.all ⇒ Array<Degrees>
All predefined degree constants.
-
.build(input) ⇒ Degrees
Build a Degrees instance from various input formats.
-
.find_by_name(search) ⇒ Degrees?
Find a constant by name or alias.
-
.parse(input) ⇒ Degrees
Parse a degrees string.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare two Degrees instances.
-
#==(other) ⇒ Boolean
Check equality.
-
#initialize(value:, name: nil, aliases: []) ⇒ Degrees
constructor
Create a new Degrees instance.
-
#opposite ⇒ Degrees
Get the opposite direction (180 degrees away).
-
#to_css ⇒ String
Convert to CSS string format.
-
#to_f ⇒ Float
Convert to float value.
-
#to_s ⇒ String
Convert to string representation.
Constructor Details
#initialize(value:, name: nil, aliases: []) ⇒ Degrees
Create a new Degrees instance.
151 152 153 154 155 |
# File 'lib/unmagic/color/units/degrees.rb', line 151 def initialize(value:, name: nil, aliases: []) @value = value.to_f % 360 @name = name @aliases = aliases end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
53 54 55 |
# File 'lib/unmagic/color/units/degrees.rb', line 53 def aliases @aliases end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
53 54 55 |
# File 'lib/unmagic/color/units/degrees.rb', line 53 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
53 54 55 |
# File 'lib/unmagic/color/units/degrees.rb', line 53 def value @value end |
Class Method Details
.all ⇒ Array<Degrees>
All predefined degree constants
59 60 61 |
# File 'lib/unmagic/color/units/degrees.rb', line 59 def all all_by_name.values.uniq end |
.build(input) ⇒ Degrees
Build a Degrees instance from various input formats.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/unmagic/color/units/degrees.rb', line 107 def build(input) case input when Degrees input when ::Numeric new(value: input) when ::String parse(input) else raise ParseError, "Expected Numeric, String, or Degrees, got #{input.class}" end end |
.find_by_name(search) ⇒ Degrees?
Find a constant by name or alias
67 68 69 70 71 72 |
# File 'lib/unmagic/color/units/degrees.rb', line 67 def find_by_name(search) normalized = search.strip.downcase all_by_name.fetch(normalized) rescue KeyError nil end |
.parse(input) ⇒ Degrees
Parse a degrees string.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/unmagic/color/units/degrees.rb', line 125 def parse(input) raise ParseError, "Input must be a string" unless input.is_a?(::String) input = input.strip # Try to find a named constant first constant = find_by_name(input) return constant if constant # Remove "deg" or "°" suffix if present input = input.sub(/deg\z/i, "").sub(/°\z/, "") # Try parsing as number if input.match?(/\A-?\d+(?:\.\d+)?\z/) return new(value: input.to_f) end raise ParseError, "Invalid degrees format: #{input.inspect}" end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compare two Degrees instances.
190 191 192 193 194 195 196 197 |
# File 'lib/unmagic/color/units/degrees.rb', line 190 def <=>(other) case other when Degrees @value <=> other.value when ::Numeric @value <=> other.to_f end end |
#==(other) ⇒ Boolean
Check equality.
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/unmagic/color/units/degrees.rb', line 203 def ==(other) case other when Degrees @value == other.value when ::Numeric @value == other.to_f else false end end |
#opposite ⇒ Degrees
Get the opposite direction (180 degrees away).
167 168 169 170 |
# File 'lib/unmagic/color/units/degrees.rb', line 167 def opposite opposite_value = (@value + 180) % 360 self.class.all.find { |d| d.value == opposite_value } || self.class.new(value: opposite_value) end |
#to_css ⇒ String
Convert to CSS string format.
175 176 177 |
# File 'lib/unmagic/color/units/degrees.rb', line 175 def to_css "#{@value}deg" end |
#to_f ⇒ Float
Convert to float value.
160 161 162 |
# File 'lib/unmagic/color/units/degrees.rb', line 160 def to_f @value end |
#to_s ⇒ String
Convert to string representation.
182 183 184 |
# File 'lib/unmagic/color/units/degrees.rb', line 182 def to_s @name || "#{@value}°" end |