Class: NeonSakura::ThemeImporter
- Inherits:
-
Object
- Object
- NeonSakura::ThemeImporter
- Defined in:
- lib/neon_sakura/theme_importer.rb
Overview
ThemeImporter provides utilities to import themes from other CSS frameworks like Bootstrap, Material UI, etc. and convert them to neon_sakura’s theme structure
Class Method Summary collapse
-
.detect_mode(theme_vars) ⇒ String
Detect light/dark mode based on background color brightness.
-
.from_bootstrap(bootstrap_vars) ⇒ Hash
Import a Bootstrap theme.
-
.from_hash(theme_hash) ⇒ Hash
Import from a generic hash of color variables.
-
.from_material_ui(material_vars) ⇒ Hash
Import a Material UI theme.
-
.to_css_variables(theme) ⇒ String
Generate CSS custom properties for a theme.
Class Method Details
.detect_mode(theme_vars) ⇒ String
Detect light/dark mode based on background color brightness
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/neon_sakura/theme_importer.rb', line 93 def self.detect_mode(theme_vars) bg = theme_vars[:background] || theme_vars[:"body-bg"] || "#ffffff" # Convert hex to RGB and calculate brightness if bg.start_with?("#") hex = bg.delete("#") r = hex[0..1].to_i(16) g = hex[2..3].to_i(16) b = hex[4..5].to_i(16) # Calculate perceived brightness (0-255) brightness = (r * 0.299 + g * 0.587 + b * 0.114) brightness > 127 ? "light" : "dark" else "light" # Default to light if we can't parse the color end end |
.from_bootstrap(bootstrap_vars) ⇒ Hash
Import a Bootstrap theme
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/neon_sakura/theme_importer.rb', line 10 def self.from_bootstrap(bootstrap_vars) { name: bootstrap_vars[:name] || "bootstrap", mode: bootstrap_vars[:mode] || detect_mode(bootstrap_vars), label: bootstrap_vars[:label] || "Bootstrap Theme", colors: { accent: bootstrap_vars[:primary] || "#0d6efd", background: bootstrap_vars[:"body-bg"] || "#ffffff", surface: bootstrap_vars[:light] || "#f8f9fa", text_primary: bootstrap_vars[:"body-color"] || "#212529", text_secondary: bootstrap_vars[:secondary] || "#6c757d", notification: bootstrap_vars[:success] || "#198754", alert: bootstrap_vars[:danger] || "#dc3545", border: bootstrap_vars[:border] || "#dee2e6" } } end |
.from_hash(theme_hash) ⇒ Hash
Import from a generic hash of color variables
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/neon_sakura/theme_importer.rb', line 54 def self.from_hash(theme_hash) { name: theme_hash[:name] || "custom", mode: theme_hash[:mode] || detect_mode(theme_hash), label: theme_hash[:label] || "Custom Theme", colors: { accent: theme_hash[:accent] || theme_hash[:primary] || "#3b82f6", background: theme_hash[:background] || theme_hash[:bg] || "#ffffff", surface: theme_hash[:surface] || theme_hash[:card] || "#f9fafb", text_primary: theme_hash[:text_primary] || theme_hash[:text] || "#111827", text_secondary: theme_hash[:text_secondary] || "#6b7280", notification: theme_hash[:notification] || theme_hash[:success] || "#10b981", alert: theme_hash[:alert] || theme_hash[:error] || theme_hash[:danger] || "#ef4444", border: theme_hash[:border] || "#e5e7eb" } } end |
.from_material_ui(material_vars) ⇒ Hash
Import a Material UI theme
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/neon_sakura/theme_importer.rb', line 31 def self.from_material_ui(material_vars) palette = material_vars[:palette] || {} { name: material_vars[:name] || "material", mode: material_vars[:mode] || (palette[:mode] == "dark" ? "dark" : "light"), label: material_vars[:label] || "Material Theme", colors: { accent: palette.dig(:primary, :main) || "#1976d2", background: palette.dig(:background, :default) || "#ffffff", surface: palette.dig(:background, :paper) || "#ffffff", text_primary: palette.dig(:text, :primary) || "rgba(0, 0, 0, 0.87)", text_secondary: palette.dig(:text, :secondary) || "rgba(0, 0, 0, 0.6)", notification: palette.dig(:success, :main) || "#2e7d32", alert: palette.dig(:error, :main) || "#d32f2f", border: palette.dig(:divider) || "rgba(0, 0, 0, 0.12)" } } end |
.to_css_variables(theme) ⇒ String
Generate CSS custom properties for a theme
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/neon_sakura/theme_importer.rb', line 75 def self.to_css_variables(theme) colors = theme[:colors] css = ":root[data-theme-name=\"#{theme[:name]}\"][data-theme-mode=\"#{theme[:mode]}\"] {\n" colors.each do |key, value| css_var_name = key.to_s.gsub("_", "-") css += " --color-#{css_var_name}: #{value};\n" end css += "}\n" css end |