A powerful JavaScript framework for implementing intelligent dark mode with automatic color adjustment and WCAG-compliant contrast optimization.
See the demo! and read the docs
- π¨ Smart Color Adjustment - Automatically adjusts colors while maintaining readability
- βΏ WCAG Compliant - Ensures 4.5:1 contrast ratio for accessibility
- πΎ Dual Storage - Uses both cookies and localStorage for maximum compatibility
- π Theme Class Support - Works with CSS-based themes or dynamic color conversion
- βοΈ Highly Configurable - Multiple options for customization
- β‘ Zero Dependencies - Pure JavaScript, no external libraries
- π Cross-Browser - Works on all modern browsers
<script src="https://cdn.jsdelivr.net/npm/blackmagic-js@latest/dist/blackmagic.min.js"></script>npm install blackmagic-js// Initialize BlackMagic
const blackMagic = new BlackMagic({
cookieName: 'my-theme',
backgroundColor: '#1a1a1a',
factor: 0.4
});
// Add toggle functionality
document.getElementById('toggleBtn').addEventListener('click', () => {
blackMagic.toggle();
});| Option | Type | Default | Description |
|---|---|---|---|
cookieName |
string | 'darkmode' |
Cookie name for storing theme preference |
backgroundColor |
string | '#000' |
Dark mode background color |
localStorageKey |
string | 'darkmode' |
localStorage key for theme storage |
themeClass |
string | undefined |
CSS class to toggle instead of dynamic colors |
cookieDuration |
number | 365 |
Cookie expiration time in days |
autoSwitch |
boolean | true |
Automatically apply saved theme on page load |
factor |
number | 0.4 |
Intensity of color adjustments (0.1-0.8) |
Switches between light and dark themes.
Returns the current theme ('light' or 'dark').
Applies a specific theme.
theme(string): Either'light'or'dark'
Returns the stored theme preference from cookies or localStorage.
const blackMagic = new BlackMagic({
themeClass: 'dark-theme',
autoSwitch: true
});const blackMagic = new BlackMagic({
backgroundColor: '#2d2d2d',
factor: 0.6, // More dramatic color changes
cookieDuration: 30 // Remember for 30 days
});This repository includes comprehensive examples demonstrating various use cases:
- Basic Example - Simple implementation
- Comprehensive Test - Full feature testing with complex UI
- Theme Class Mode - Using CSS classes instead of dynamic colors
- Factor Testing - High/low intensity color adjustments
- Settings Tests - Auto-switch, custom backgrounds, cookie duration
- Debug Tools - Persistence testing and troubleshooting
# Clone the repository
git clone https://github.com/LucAngevare/BlackMagic-js.git
cd BlackMagic-js
# Start local server
npm run dev
# Open examples
open http://localhost:8000/examples/Or just look at the already running examples at blackmagic.lucangevare.nl.
BlackMagic-js/
βββ src/
β βββ blackmagic.js # Source code
βββ dist/
β βββ blackmagic.js # UMD build
β βββ blackmagic.esm.js # ES Module
βββ examples/
β βββ basic/ # Basic usage examples
β βββ comprehensive/ # Full feature tests
β βββ theme-class/ # CSS class mode
β βββ factors/ # Color adjustment tests
β βββ settings/ # Configuration tests
β βββ debug/ # Debug tools
βββ package.json
βββ README.md
BlackMagic uses advanced algorithms to:
- Analyze Text Colors - Detects current text colors in the DOM
- Calculate Backgrounds - Walks up the DOM tree to find actual background colors
- Ensure Contrast - Calculates contrast ratios using WCAG standards
- Optimize Colors - Adjusts colors to maintain 4.5:1 contrast ratio
- Preserve Semantics - Keeps important UI colors (buttons, alerts) unchanged
The framework uses a dual storage approach:
- Cookies (Primary) - Works across all environments
- localStorage (Fallback) - Provides additional reliability
All color adjustments ensure WCAG AA compliance:
- Minimum 4.5:1 contrast ratio
- Proper luminance calculations
- Gamma correction applied
- Fallback to pure black/white when needed
- β Chrome 60+
- β Firefox 60+
- β Safari 12+
- β Edge 79+
- β Opera 47+
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- WCAG guidelines for accessibility standards
- Modern CSS color theory and contrast calculations
- Community feedback and testing
- π Documentation
- π Issues
- π¬ Discussions
β If you find BlackMagic useful, please consider giving it a star! β