Class: Uniword::Drawingml::ColorScheme

Inherits:
Lutaml::Model::Serializable
  • Object
show all
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.

Examples:

Create a custom color scheme

scheme = Uniword::ColorScheme.new
scheme.colors[:accent1] = '0066CC'  # Corporate blue
scheme.colors[:accent2] = 'FF6600'  # Corporate orange

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

Constructor Details

#initialize(attributes = {}) ⇒ ColorScheme

Initialize color scheme

Parameters:

  • attributes (Hash) (defaults to: {})

    Color scheme attributes



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

Parameters:

  • color_name (String, Symbol)

    The color name (e.g., :accent1)

Returns:

  • (String, nil)

    The RGB hex color value



312
313
314
315
316
317
318
# File 'lib/uniword/drawingml/color_scheme.rb', line 312

def [](color_name)
  color_name = color_name.to_sym
  # Map folHlink to fol_hlink for attribute access
  attr_name = color_name == :folHlink ? :fol_hlink : color_name
  color_obj = instance_variable_get("@#{attr_name}")
  color_obj&.value
end

#[]=(color_name, value) ⇒ Object

Set a color by name

Parameters:

  • color_name (String, Symbol)

    The color name

  • value (String)

    The RGB hex color value



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/uniword/drawingml/color_scheme.rb', line 324

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
  instance_variable_set("@#{attr_name}", color_obj)
  @colors_hash[color_name] = value
end

#all_colorsHash

Get all defined colors

Returns:

  • (Hash)

    All colors



355
356
357
358
# File 'lib/uniword/drawingml/color_scheme.rb', line 355

def all_colors
  sync_colors_hash
  @colors_hash
end

#colorsObject

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

#dupColorScheme

Duplicate the color scheme

Returns:



371
372
373
374
375
376
377
378
# File 'lib/uniword/drawingml/color_scheme.rb', line 371

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

Parameters:

  • color_name (String, Symbol)

    The color name

Returns:

  • (Boolean)

    true if color is defined



364
365
366
# File 'lib/uniword/drawingml/color_scheme.rb', line 364

def has_color?(color_name)
  self[color_name] != nil
end

#sync_colors_hashObject

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