Git Product home page Git Product logo

tailwindcss-color-palette-generator's Introduction

TailwindCSS Color Palette Generator

Generates TailwindCSS color palette (ranging from 50 to 900) from a single base color (which can be given as a hex value, HSL or RGB).

Uses HSL color model and base color lightness to generate step colors by raising (or lowering) lightness in steps (and considering bound thresholds).

Installation

Install it via composer by running:

composer require lukapeharda/tailwindcss-color-palette-generator

Usage

First, import needed namespaces and classes:

use LukaPeharda\TailwindCssColorPaletteGenerator\Color;
use LukaPeharda\TailwindCssColorPaletteGenerator\PaletteGenerator;

Then create your base color:

// from hex
$baseColor = Color::fromHex('#ffff00');

// or from RGB
$baseColor = Color::fromRgb(255, 255, 0);

// or from HSL
$baseColor = Color::fromHsl(60, 100, 50); // or $baseColor = Color::fromHsl(0.6, 1, 0.5);

Lastly, use base color to create a color palette:

$paletteGenerator = new PaletteGenerator;
$paletteGenerator->setBaseColor($baseColor);
$palette = $paletteGenerator->getPalette();

Generated $palette will be an array where keys are TailwindCSS color steps and values Color objects:

$palette = [
    50 => Color,
    100 => Color,
    200 => Color,
    300 => Color,
    ...
];

You can then loop over it to generate CSS variables or use it anyway you see fit:

foreach ($palette as $key => $color) {
    echo '--color-primary-' . $key . ': #' . $color->getHex() . ';';
}

Extend color settings in your tailwind.config.js file and add primary color pallete:

module.exports = {
    theme: {
        extend: {
            colors: {
                primary: {
                    50: 'var(--color-primary-50, #F5F3FF)',
                    100: 'var(--color-primary-100, #EDE9FE)',
                    200: 'var(--color-primary-200, #DDD6FE)',
                    300: 'var(--color-primary-300, #C4B5FD)',
                    400: 'var(--color-primary-400, #A78BFA)',
                    500: 'var(--color-primary-500, #8B5CF6)',
                    600: 'var(--color-primary-600, #7C3AED)',
                    700: 'var(--color-primary-700, #6D28D9)',
                    800: 'var(--color-primary-800, #5B21B6)',
                    900: 'var(--color-primary-900, #4C1D95)',
                }
            }
        }
    }
}

Afterwards you can use your color as regular CSS Tailwind class, for example as text-primary-100 or bg-primary-300.

Defaults

PaletteGenerator class has some configurable options set to a sensible defaults.

Base color step value

By default base color step value is 500. This means that 5 lighter colors and 4 darker will be generated in a palette.

You can change it by calling setBaseValue method on the PaletteGenerator object:

$paletteGenerator->setBaseValue(400);

By setting the base value to 400 4 lighter colors and 5 darker will be generated.

Bounds thresholds

By default the lightest generated color will have a lightness value of 90% and the darkest will have the lightness value of 10%.

You can change this by calling setThresholdLightest and setThresholdDarkest methods on PaletteGenerator object:

$paletteGenerator->setThresholdLightest(80); // or $paletteGenerator->setThresholdLightest(0.8);
$paletteGenerator->setThresholdDarkest(20); // or $paletteGenerator->setThresholdDarkest(0.2);

Color steps

By default, TailwindCSS 2.x color steps range is used (from 50 to 900).

You can override it by calling setColorSteps method on PaletteGenerator object by giving it an array with step values:

$paletteGenerator->setColorSteps([100, 200, 300, 400, 500, 600, 700]);

Todos

Different strategies for generating colors are planned to be developed.

tailwindcss-color-palette-generator's People

Contributors

lukapeharda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tailwindcss-color-palette-generator's Issues

Missing 950 and Divide by Zero Errors

Awesome library, thanks for sharing.

I might jump in here and fix these over the next few weeks, but no time at the moment. So just documenting for now.

  1. Tailwind classes should go 50-950, but currently hardcoded 50-900.
  2. Pure Black/White seems to fail in current library
  3. Library currently sets all colors to 500, which makes for an inconsistent palette
  4. Library seems to fail with very dark or very light colors

I'm using this library in conjunction with PHPLeague color extrator to get the dominant color from an image, then convert to a tailwind palette, which can then dynamically theme parts of a site (using this concept, similar to yours tailwindlabs/tailwindcss#12640)

I'm more or less trying to emulate https://uicolors.app/create which works exceptionally well with all colors.

I've created a few methods to auto-adjust based on HSL, and when you adjust those colors above to 900 you get the Divide By Zero errors:

generateTailwindPalette('#06104B', 900)
generateTailwindPalette('#0B0E2D', 900)

I'll create a PR when I figure it out.

$this when not in object context

Getting

Fatal error: Uncaught Error: Using $this when not in object context in /Users/jamesauble/Projects/wauble/wp-content/themes/wauble-tailwind/vendor/lukapeharda/tailwindcss-color-palette-generator/src/Color.php:85 Stack trace: #0 /Users/jamesauble/Projects/wauble/wp-content/themes/wauble-tailwind/template-parts/theme-css-vars.php(9): LukaPeharda\TailwindCssColorPaletteGenerator\Color::fromRgb(255, 255, 0) #1 /Users/jamesauble/Projects/wauble/wp-includes/template.php(772): require('/Users/jamesaub...') #2 /Users/jamesauble/Projects/wauble/wp-includes/template.php(716): load_template('/Users/jamesaub...', false, Array) #3 /Users/jamesauble/Projects/wauble/wp-includes/general-template.php(204): locate_template(Array, true, false, Array) #4 /Users/jamesauble/Projects/wauble/wp-content/themes/wauble-tailwind/template-parts/header.php(8): get_template_part('template-parts/...') #5 /Users/jamesauble/Projects/wauble/wp-includes/template.php(772): require('/Users/jamesaub...') #6 /Users/jamesauble/Projects/wauble/wp-includes/template.php(716): load_template('/Users/jamesaub...', false, Array) #7 /Users/jamesauble/Projects/wauble/wp-includes/general-template.php(204): locate_template(Array, true, false, Array) #8 /Users/jamesauble/Projects/wauble/wp-content/themes/wauble-tailwind/front-page.php(1): get_template_part('template-parts/...') #9 /Users/jamesauble/Projects/wauble/wp-includes/template-loader.php(106): include('/Users/jamesaub...') #10 /Users/jamesauble/Projects/wauble/wp-blog-header.php(19): require_once('/Users/jamesaub...') #11 /Users/jamesauble/Projects/wauble/index.php(17): require('/Users/jamesaub...') #12 {main} thrown in /Users/jamesauble/Projects/wauble/wp-content/themes/wauble-tailwind/vendor/lukapeharda/tailwindcss-color-palette-generator/src/Color.php on line 85

This some new strictness with PHP 8? I thought you couldn't use $this in a static function but I'm not very great at PHP OOP.

PHP 8 Deprecation: Implicit conversion from float to int loses precision

Hi there,

I'm hitting the following error, when trying to run getHex() on a Color. Latest version of the package + PHP v8.0.11:

PHP Deprecated: Implicit conversion from float 0.5759627129540341 to int loses precision in A:\laragon\www\twenty-for\vendor\lukapeharda\tailwindcss-color-palette-generator\src\Color.php on line 234

Here's the method & hexcode I'm running:

$paletteGenerator = new PaletteGenerator;
$paletteGenerator->setBaseColor( Color::fromHex('007a33') );
return $paletteGenerator->getPalette();

Thanks for building this package! Handy concept, if I'm able to get it working. Let me know if there's anything else I can provide or help with, here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.