Class: Uniword::Drawingml::ColorScheme
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Uniword::Drawingml::ColorScheme
- Defined in:
- lib/uniword/drawingml/color_scheme.rb
Overview
Represents a color scheme from a Word document theme.
Color schemes define the theme colors used throughout the document. There are 12 standard theme colors in Office Open XML.
Defined Under Namespace
Classes: ColorSchemeHash
Constant Summary collapse
- THEME_COLORS =
The 12 standard theme colors defined in OOXML
%w[ dk1 lt1 dk2 lt2 accent1 accent2 accent3 accent4 accent5 accent6 hlink folHlink ].freeze
Instance Method Summary collapse
-
#[](color_name) ⇒ String?
Get a color by name.
-
#[]=(color_name, value) ⇒ Object
Set a color by name.
-
#all_colors ⇒ Hash
Get all defined colors.
- #assign_color_by_name(name, color_obj) ⇒ Object
- #color_by_name(name) ⇒ Object
-
#colors ⇒ Object
Getter for hash-like color access Returns a proxy that delegates writes to the ColorScheme.
-
#dup ⇒ ColorScheme
Duplicate the color scheme.
-
#has_color?(color_name) ⇒ Boolean
Check if color scheme has a specific color defined.
-
#initialize(attributes = {}) ⇒ ColorScheme
constructor
Initialize color scheme.
-
#sync_colors_hash ⇒ Object
Sync the hash interface with attribute values.
Constructor Details
#initialize(attributes = {}) ⇒ ColorScheme
Initialize color scheme
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 246 def initialize(attributes = {}) super @colors_hash = {} # Initialize color objects with default values @dk1 ||= Dk1Color.new @dk1.rgb = "000000" if @dk1.srgb_clr.nil? && @dk1.sys_clr.nil? @lt1 ||= Lt1Color.new @lt1.rgb = "FFFFFF" if @lt1.srgb_clr.nil? && @lt1.sys_clr.nil? @dk2 ||= Dk2Color.new @dk2.rgb = "44546A" if @dk2.srgb_clr.nil? && @dk2.sys_clr.nil? @lt2 ||= Lt2Color.new @lt2.rgb = "E7E6E6" if @lt2.srgb_clr.nil? && @lt2.sys_clr.nil? @accent1 ||= Accent1Color.new @accent1.rgb = "4472C4" if @accent1.srgb_clr.nil? && @accent1.sys_clr.nil? @accent2 ||= Accent2Color.new @accent2.rgb = "ED7D31" if @accent2.srgb_clr.nil? && @accent2.sys_clr.nil? @accent3 ||= Accent3Color.new @accent3.rgb = "A5A5A5" if @accent3.srgb_clr.nil? && @accent3.sys_clr.nil? @accent4 ||= Accent4Color.new @accent4.rgb = "FFC000" if @accent4.srgb_clr.nil? && @accent4.sys_clr.nil? @accent5 ||= Accent5Color.new @accent5.rgb = "5B9BD5" if @accent5.srgb_clr.nil? && @accent5.sys_clr.nil? @accent6 ||= Accent6Color.new @accent6.rgb = "70AD47" if @accent6.srgb_clr.nil? && @accent6.sys_clr.nil? @hlink ||= HlinkColor.new @hlink.rgb = "0563C1" if @hlink.srgb_clr.nil? && @hlink.sys_clr.nil? @fol_hlink ||= FolHlinkColor.new @fol_hlink.rgb = "954F72" if @fol_hlink.srgb_clr.nil? && @fol_hlink.sys_clr.nil? # Build hash interface sync_colors_hash end |
Instance Method Details
#[](color_name) ⇒ String?
Get a color by name
312 313 314 315 316 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 312 def [](color_name) attr_name = color_name.to_sym == :folHlink ? :fol_hlink : color_name.to_sym color_obj = color_by_name(attr_name) color_obj&.value end |
#[]=(color_name, value) ⇒ Object
Set a color by name
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 339 def []=(color_name, value) color_name = color_name.to_sym # Map folHlink to fol_hlink for attribute access attr_name = color_name == :folHlink ? :fol_hlink : color_name # Create appropriate color class instance color_class = case attr_name when :dk1 then Dk1Color when :lt1 then Lt1Color when :dk2 then Dk2Color when :lt2 then Lt2Color when :accent1 then Accent1Color when :accent2 then Accent2Color when :accent3 then Accent3Color when :accent4 then Accent4Color when :accent5 then Accent5Color when :accent6 then Accent6Color when :hlink then HlinkColor when :fol_hlink then FolHlinkColor else Dk1Color end color_obj = color_class.new color_obj.rgb = value assign_color_by_name(attr_name, color_obj) @colors_hash[color_name] = value end |
#all_colors ⇒ Hash
Get all defined colors
387 388 389 390 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 387 def all_colors sync_colors_hash @colors_hash end |
#assign_color_by_name(name, color_obj) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 367 def assign_color_by_name(name, color_obj) case name when :dk1 then self.dk1 = color_obj when :lt1 then self.lt1 = color_obj when :dk2 then self.dk2 = color_obj when :lt2 then self.lt2 = color_obj when :accent1 then self.accent1 = color_obj when :accent2 then self.accent2 = color_obj when :accent3 then self.accent3 = color_obj when :accent4 then self.accent4 = color_obj when :accent5 then self.accent5 = color_obj when :accent6 then self.accent6 = color_obj when :hlink then self.hlink = color_obj when :fol_hlink then self.fol_hlink = color_obj end end |
#color_by_name(name) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 318 def color_by_name(name) case name when :dk1 then dk1 when :lt1 then lt1 when :dk2 then dk2 when :lt2 then lt2 when :accent1 then accent1 when :accent2 then accent2 when :accent3 then accent3 when :accent4 then accent4 when :accent5 then accent5 when :accent6 then accent6 when :hlink then hlink when :fol_hlink then fol_hlink end end |
#colors ⇒ Object
Getter for hash-like color access Returns a proxy that delegates writes to the ColorScheme
239 240 241 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 239 def colors @colors ||= ColorSchemeHash.new(self) end |
#dup ⇒ ColorScheme
Duplicate the color scheme
403 404 405 406 407 408 409 410 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 403 def dup new_scheme = ColorScheme.new(name: name) THEME_COLORS.each do |color_name| value = self[color_name] new_scheme[color_name] = value if value end new_scheme end |
#has_color?(color_name) ⇒ Boolean
Check if color scheme has a specific color defined
396 397 398 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 396 def has_color?(color_name) self[color_name] != nil end |
#sync_colors_hash ⇒ Object
Sync the hash interface with attribute values
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/uniword/drawingml/color_scheme.rb', line 291 def sync_colors_hash @colors_hash = { dk1: @dk1&.value, lt1: @lt1&.value, dk2: @dk2&.value, lt2: @lt2&.value, accent1: @accent1&.value, accent2: @accent2&.value, accent3: @accent3&.value, accent4: @accent4&.value, accent5: @accent5&.value, accent6: @accent6&.value, hlink: @hlink&.value, folHlink: @fol_hlink&.value, }.compact end |