npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tw-color-shades

v1.0.2

Published

Generate Tailwind-compatible shades from a single color. Supported color formats: hex, rgb and hsl.

Downloads

72

Readme

tw-color-shades: TailwindCSS Shades Generator

A lightweight JavaScript utility to generate TailwindCSS-compatible color shades from a single color input. The library supports various color formats such as HEX, RGB, and HSL, and can also handle CSS variables for enhanced flexibility.

Features

  • Multiple color format support:
    • HEX: #xxx, #xxxxxx, #xxxxxxxx
    • RGB: rgb(xxx, xxx, xxx), rgb(xxx xxx xxx)
    • HSL: hsl(xxx, xx%, xx%), hsl(xdeg, x%, x%)
  • CSS variable support:
    • Converts CSS variables to rgb(var(--color) / <alpha-value>) format for Tailwind opacity. CSS variables will not generate shades!
  • Custom shade generation:
    • Automatically generates shades: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950.

Installation

You can install tw-color-shades via npm or yarn:

npm install tw-color-shades
# or
yarn add tw-color-shades

Usage

Use within any Javascript

const twShades = require('tw-color-shades');
const shades = twShades('#3498db');
console.log(shades);

// Output:
{
  50: 'rgb(234, 246, 253)',
  ...
  500: 'rgb(52, 152, 219)',
  ...
  950: 'rgb(0, 40, 89)'
}

// Use CSS var
const color_with_opacity_support = twShades('--ui-base-color');
console.log(color_with_opacity_support);

// Output:
"rgb(var(--ui-base-color) / <alpha-value>)"

Use within tailwind.config.cjs

Import the library into your tailwind.config.cjs file and use it like below:

/** @type {import('tailwindcss').Config} */
const twShades = require('tw-color-shades');

module.exports = {
  content: [],
  theme: {
    extend: {
      colors: {
        brand: twShades('#3498db'),   // HEX color shades
        primary: twShades('--ui-base-color'),  // CSS variable for opacity
      }
    }
  },
  plugins: []
};

The above code will generate the following Tailwind classes:

/* Use of the Hex color shades */
class="X-brand-50"
...
class="X-brand-500"
...
class="X-brand-950"

/* use of the CSS var */
class="bg-primary/60"

Replace X with any valid Tailwind color utility, such as bg, text, border, or fill.

The CSS var will not generates shades! Instead, it will be compatible with opacity-based color classes like bg-primary/50

API Documentation

twShades(color: string): { [key: number]: string } Generates Tailwind CSS-compatible shades from a single color.

Parameters

  • color(string): The input color value in HEX, RGB, HSL format, or a CSS variable.

Returns

  • An object where the keys represent the shade values (e.g., 50, 100) and the values are the corresponding colors in RGB format.
  • For CSS variables, it returns a string compatible with Tailwind CSS opacity utilities.

Throws

  • An error if the color format is not recognized or invalid.

Contributing

  1. Fork the repository.
  2. Create a new feature branch.
  3. Commit your changes.
  4. Open a pull request.

Copyright

The code and documentation are copyright 2024 by M B Parvez.

License

This project is licensed under the MIT License.