Class: GD::GIS::Style
- Inherits:
-
Object
- Object
- GD::GIS::Style
- Defined in:
- lib/gd/gis/style.rb
Overview
Defines visual styling rules for map rendering.
A Style object encapsulates all visual configuration used during rendering, including colors, stroke widths, fonts, and layer ordering.
Styles are typically loaded from YAML files and applied to a Map instance before rendering.
Instance Attribute Summary collapse
-
#global ⇒ Hash
readonly
Global styling rules.
-
#order ⇒ Array<Symbol>
readonly
Drawing order of semantic layers.
-
#parks ⇒ Hash
readonly
Park styling rules.
-
#point ⇒ Hash
readonly
Point styling rules.
-
#points ⇒ Hash
readonly
Point styling rules.
-
#rails ⇒ Hash
readonly
Rail styling rules.
-
#roads ⇒ Hash
readonly
Road styling rules.
-
#track ⇒ Hash
readonly
Track styling rules.
-
#water ⇒ Hash
readonly
Water styling rules.
Class Method Summary collapse
-
.deep_symbolize(obj) ⇒ Object
Recursively converts hash keys to symbols.
-
.default ⇒ Style
Returns a built-in default style so Map can render even if no external style is set.
-
.load(name, from: "styles") ⇒ Style
Loads a style definition from a YAML file.
Instance Method Summary collapse
-
#initialize(definition) ⇒ Style
constructor
Creates a new style from a definition hash.
-
#normalize_color(color) ⇒ GD::Color
Normalizes a color definition into a GD::Color.
Constructor Details
#initialize(definition) ⇒ Style
Creates a new style from a definition hash.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/gd/gis/style.rb', line 49 def initialize(definition) @global = definition[:global] || {} @point = definition[:point] || {} @roads = definition[:roads] || {} @rails = definition[:rails] || {} @water = definition[:water] || {} @parks = definition[:parks] || {} @points = definition[:points] || {} @track = definition[:track] || {} @order = definition[:order] || [] end |
Instance Attribute Details
#global ⇒ Hash (readonly)
Returns global styling rules.
18 19 20 |
# File 'lib/gd/gis/style.rb', line 18 def global @global end |
#order ⇒ Array<Symbol> (readonly)
Returns drawing order of semantic layers.
42 43 44 |
# File 'lib/gd/gis/style.rb', line 42 def order @order end |
#parks ⇒ Hash (readonly)
Returns park styling rules.
33 34 35 |
# File 'lib/gd/gis/style.rb', line 33 def parks @parks end |
#point ⇒ Hash (readonly)
Returns point styling rules.
21 22 23 |
# File 'lib/gd/gis/style.rb', line 21 def point @point end |
#points ⇒ Hash (readonly)
Returns point styling rules.
36 37 38 |
# File 'lib/gd/gis/style.rb', line 36 def points @points end |
#rails ⇒ Hash (readonly)
Returns rail styling rules.
27 28 29 |
# File 'lib/gd/gis/style.rb', line 27 def rails @rails end |
#roads ⇒ Hash (readonly)
Returns road styling rules.
24 25 26 |
# File 'lib/gd/gis/style.rb', line 24 def roads @roads end |
#track ⇒ Hash (readonly)
Returns track styling rules.
39 40 41 |
# File 'lib/gd/gis/style.rb', line 39 def track @track end |
#water ⇒ Hash (readonly)
Returns water styling rules.
30 31 32 |
# File 'lib/gd/gis/style.rb', line 30 def water @water end |
Class Method Details
.deep_symbolize(obj) ⇒ Object
Recursively converts hash keys to symbols.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gd/gis/style.rb', line 101 def self.deep_symbolize(obj) case obj when Hash obj.transform_keys(&:to_sym) .transform_values { |v| deep_symbolize(v) } when Array obj.map { |v| deep_symbolize(v) } else obj end end |
.default ⇒ Style
Returns a built-in default style so Map can render even if no external style is set.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/gd/gis/style.rb', line 116 def self.default new({ global: { background: [15, 23, 42], font: GD::GIS::FontHelper.find("DejaVuSans"), font_color: [243, 244, 246], label: { color: [229, 231, 235], font: GD::GIS::FontHelper.find("DejaVuSans"), size: 12 } }, label: { label: { color: [229, 231, 235], font: GD::GIS::FontHelper.find("DejaVuSans"), size: 12 } }, roads: { roads: { color: [229, 231, 235], font: GD::GIS::FontHelper.find("DejaVuSans"), width: 6 } }, rails: { rails: { color: [156, 163, 175], font: GD::GIS::FontHelper.find("DejaVuSans"), width: 5 } }, water: { water: { color: [59, 130, 246] } }, parks: { parks: { color: [34, 197, 94] } }, points: { points: { color: [239, 68, 68], radius: 4, font: GD::GIS::FontHelper.find("DejaVuSans"), font_color: [243, 244, 246] } }, track: { track: { stroke: [0, 85, 127, 250], color: [250, 204, 21], width: 2, font_color: [243, 244, 246] } }, order: %i[water parks rails roads points track] }) end |
.load(name, from: "styles") ⇒ Style
Loads a style definition from a YAML file.
The file name is resolved as:
<from>/<name>.yml
All keys are deep-symbolized on load.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gd/gis/style.rb', line 77 def self.load(name, from: "styles") path = File.join(from, "#{name}.yml") raise "Style not found: #{path}" unless File.exist?(path) data = YAML.load_file(path) data = deep_symbolize(data) new( global: data[:global], point: data[:point], roads: data[:roads], rails: data[:rail] || data[:rails], track: data[:track], water: data[:water], parks: data[:park] || data[:parks], points: data[:points], order: (data[:order] || []).map(&:to_sym) ) end |
Instance Method Details
#normalize_color(color) ⇒ GD::Color
Normalizes a color definition into a GD::Color.
Accepted formats:
-
GD::Color instance
- r, g, b
- r, g, b, a
-
nil (generates a random vivid color)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/gd/gis/style.rb', line 198 def normalize_color(color) case color when GD::Color color when Array case color.length when 3 GD::Color.rgb(*color) when 4 GD::Color.rgba(*color) else raise ArgumentError, "Style error: color array must be [r,g,b] or [r,g,b,a]" end when nil GD::GIS::ColorHelpers.random_vivid else raise ArgumentError, "Style error: invalid color format (#{color.inspect})" end end |