Class: Unmagic::Color::RGB::Named::Database Private

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/color/rgb/named.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Database for loading and accessing color data from files.

Handles lazy loading, name normalization, and color lookup.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, name: nil, aliases: []) ⇒ Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new color database.

Parameters:

  • path (String)

    Path to the database file

  • name (String, nil) (defaults to: nil)

    The name of the database (e.g., “x11”, “css”)

  • aliases (Array<String>) (defaults to: [])

    Alternative names for the database



56
57
58
59
60
61
# File 'lib/unmagic/color/rgb/named.rb', line 56

def initialize(path:, name: nil, aliases: [])
  @filepath = path
  @name = name
  @aliases = aliases
  @data = nil
end

Instance Attribute Details

#aliasesArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Alternative names for the database.

Returns:

  • (Array<String>)

    Alternative names for the database



49
50
51
# File 'lib/unmagic/color/rgb/named.rb', line 49

def aliases
  @aliases
end

#nameString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The name of the database.

Returns:

  • (String, nil)

    The name of the database



45
46
47
# File 'lib/unmagic/color/rgb/named.rb', line 45

def name
  @name
end

Instance Method Details

#[](color_name) ⇒ RGB?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lookup color by name, returns RGB color or nil.

Parameters:

  • color_name (String)

    The color name to lookup

Returns:

  • (RGB, nil)

    The RGB color instance or nil if not found



67
68
69
70
71
# File 'lib/unmagic/color/rgb/named.rb', line 67

def [](color_name)
  normalized = normalize_name(color_name)
  int_value = data[normalized]
  int_value ? RGB.build(int_value) : nil
end

#allArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all color names in database.

Returns:

  • (Array<String>)

    Array of all color names



85
86
87
# File 'lib/unmagic/color/rgb/named.rb', line 85

def all
  data.keys
end

#loaded?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if database has been loaded.

Returns:

  • (Boolean)

    true if data has been loaded from file



92
93
94
# File 'lib/unmagic/color/rgb/named.rb', line 92

def loaded?
  !@data.nil?
end

#memsizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate memory size of loaded database.

Returns:

  • (Integer)

    Memory size in bytes



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/unmagic/color/rgb/named.rb', line 100

def memsize
  require "objspace"

  memory = ObjectSpace.memsize_of(data)

  data.each do |key, value|
    memory += ObjectSpace.memsize_of(key)
    memory += ObjectSpace.memsize_of(value)
  end

  memory
end

#valid?(color_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if color exists in database.

Parameters:

  • color_name (String)

    The color name to check

Returns:

  • (Boolean)

    true if color exists



77
78
79
80
# File 'lib/unmagic/color/rgb/named.rb', line 77

def valid?(color_name)
  normalized = normalize_name(color_name)
  data.key?(normalized)
end