Class: Ea::Svg::StyleResolver
- Inherits:
-
Object
- Object
- Ea::Svg::StyleResolver
- Defined in:
- lib/ea/svg/style_resolver.rb
Overview
Translates the parsed style hash (from Ea::Sources::Qea::DiagramStyleParser, or the equivalent for XMI) into SVG-friendly attributes. EA packs colors as integers (e.g. 16777215 = white); we translate to CSS hex.
Constant Summary collapse
- DEFAULT_FILL =
"#ffffff"- DEFAULT_STROKE =
"#000000"- DEFAULT_FONT_COLOR =
"#000000"
Instance Attribute Summary collapse
-
#style ⇒ Object
readonly
Returns the value of attribute style.
Instance Method Summary collapse
-
#color_from_ea(raw) ⇒ Object
EA stores colors as decimal integers in BGR byte order (low byte = red, high byte = blue).
- #fill_color ⇒ Object
- #font_color ⇒ Object
-
#initialize(style_hash) ⇒ StyleResolver
constructor
A new instance of StyleResolver.
- #stroke_color ⇒ Object
- #stroke_width ⇒ Object
- #to_svg_attrs ⇒ Object
Constructor Details
#initialize(style_hash) ⇒ StyleResolver
Returns a new instance of StyleResolver.
16 17 18 |
# File 'lib/ea/svg/style_resolver.rb', line 16 def initialize(style_hash) @style = style_hash || {} end |
Instance Attribute Details
#style ⇒ Object (readonly)
Returns the value of attribute style.
14 15 16 |
# File 'lib/ea/svg/style_resolver.rb', line 14 def style @style end |
Instance Method Details
#color_from_ea(raw) ⇒ Object
EA stores colors as decimal integers in BGR byte order (low byte = red, high byte = blue). The high byte may hold alpha in some variants; we treat anything above 0xFFFFFF as opaque.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ea/svg/style_resolver.rb', line 53 def color_from_ea(raw) return nil if raw.nil? || raw.to_s.empty? # Hex strings (e.g. from XMI): passthrough. return "##{raw}" if raw.to_s.match?(/\A[0-9a-fA-F]{6}\z/) Integer(raw.to_s) rescue ArgumentError nil else # Convert BGR integer → RGB hex. value = Integer(raw) b = (value >> 16) & 0xff g = (value >> 8) & 0xff r = value & 0xff format("#%02X%02X%02X", r, g, b) end |
#fill_color ⇒ Object
20 21 22 |
# File 'lib/ea/svg/style_resolver.rb', line 20 def fill_color color_from_ea(style[:bcol]) || DEFAULT_FILL end |
#font_color ⇒ Object
28 29 30 |
# File 'lib/ea/svg/style_resolver.rb', line 28 def font_color color_from_ea(style[:fcol]) || DEFAULT_FONT_COLOR end |
#stroke_color ⇒ Object
24 25 26 |
# File 'lib/ea/svg/style_resolver.rb', line 24 def stroke_color color_from_ea(style[:lcol]) || DEFAULT_STROKE end |
#stroke_width ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/ea/svg/style_resolver.rb', line 32 def stroke_width raw = style[:lwd] return 1 if raw.nil? || raw.to_s.empty? Integer(raw) rescue ArgumentError 1 end |
#to_svg_attrs ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ea/svg/style_resolver.rb', line 41 def to_svg_attrs { fill: fill_color, stroke: stroke_color, "stroke-width": stroke_width } end |