Class: KozenetUi::Theme::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/kozenet_ui/theme/palette.rb

Overview

Dynamic color palette system with auto-generated shades Supports light/dark modes and custom brand colors

Examples:

KozenetUi::Theme::Palette.new(primary: "#123456")

Constant Summary collapse

DEFAULT_COLORS =
{
  primary: "#6366f1",    # Indigo
  secondary: "#8b5cf6",  # Purple
  accent: "#06b6d4",     # Cyan
  success: "#10b981",    # Green
  warning: "#f59e0b",    # Amber
  error: "#ef4444",      # Red
  info: "#0ea5e9"        # Sky
}.freeze
NEUTRALS_LIGHT =

Neutral colors (semantic)

{
  bg_base: "#ffffff",
  bg_elevated: "#f8fafc",
  bg_muted: "#f1f5f9",
  text_default: "#0f172a",
  text_muted: "#64748b",
  border_default: "#e2e8f0",
  border_muted: "#f1f5f9"
}.freeze
NEUTRALS_DARK =
{
  bg_base: "#101318",
  bg_elevated: "#181c23",
  bg_muted: "#242a35",
  text_default: "#f8fafc",
  text_muted: "#cbd5e1",
  border_default: "#303744",
  border_muted: "#242a35"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_colors = {}) ⇒ Palette

Returns a new instance of Palette.



46
47
48
# File 'lib/kozenet_ui/theme/palette.rb', line 46

def initialize(custom_colors = {})
  @colors = DEFAULT_COLORS.merge(custom_colors)
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



44
45
46
# File 'lib/kozenet_ui/theme/palette.rb', line 44

def colors
  @colors
end

Instance Method Details

#to_css_variables(mode: :light) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kozenet_ui/theme/palette.rb', line 51

def to_css_variables(mode: :light)
  variables = []

  # Brand colors with shades (only for valid hex colors)
  @colors.each do |name, hex|
    next if name.to_s.start_with?("gradient_spot_")
    next if name.to_s.end_with?("_dark")

    if hex.is_a?(String) && hex.match?(/^#(?:[0-9a-fA-F]{3}){1,2}$/)
      shades = generate_shades(hex)
      shades.each do |shade, color|
        variables << "--kz-#{name}-#{shade}: #{color};"
      end
    else
      # For non-hex, just output as a base variable
      variables << "--kz-#{name}: #{hex};"
    end
  end

  # Semantic neutrals
  neutrals = mode == :dark ? NEUTRALS_DARK : NEUTRALS_LIGHT
  neutrals.each do |name, value|
    variables << "--kz-#{name.to_s.tr("_", "-")}: #{value};"
  end
  variables.concat(neutral_aliases)

  # Gradient tokens (allow user to override or fallback to palette)
  if mode == :dark
    variables << "--gradient-base-from: #{@colors[:gradient_from_dark] || "#181c2a"};"
    variables << "--gradient-base-to: #{@colors[:gradient_to_dark] || "#23283a"};"
    variables << "--gradient-accent-from: #{@colors[:gradient_accent_from_dark] || "#1e40af"};"
    variables << "--gradient-accent-via: #{@colors[:gradient_accent_via_dark] || "#0369a1"};"
    variables << "--gradient-accent-to: #{@colors[:gradient_accent_to_dark] || "#0891b2"};"
    variables << "--gradient-spot-1: #{@colors[:gradient_spot_1_dark] || "rgba(56,189,248,0.20)"};"
    variables << "--gradient-spot-2: #{@colors[:gradient_spot_2_dark] || "rgba(99,102,241,0.18)"};"
  else
    variables << "--gradient-base-from: #{@colors[:gradient_from] || "#f0f9ff"};"
    variables << "--gradient-base-to: #{@colors[:gradient_to] || "#e0f2fe"};"
    variables << "--gradient-accent-from: #{@colors[:gradient_accent_from] || "#6366f1"};"
    variables << "--gradient-accent-via: #{@colors[:gradient_accent_via] || "#0ea5e9"};"
    variables << "--gradient-accent-to: #{@colors[:gradient_accent_to] || "#06b6d4"};"
    variables << "--gradient-spot-1: #{@colors[:gradient_spot_1] || "rgba(99,102,241,0.35)"};"
    variables << "--gradient-spot-2: #{@colors[:gradient_spot_2] || "rgba(14,165,233,0.30)"};"
    # rubocop:enable Naming/VariableNumber
  end

  variables.join("\n    ")
end