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

palette-by-numbers

v0.1.6

Published

Easily Generate a Tailwind/Chakra-UI Style Color Palette

Downloads

844

Readme

Tests

npm

Palette by Numbers is a collection of utilities to streamline the process of creating color palettes for design systems such as Chakra UI and Tailwind CSS. When adding custom theme colors to these amazing projects, it's usually required to supply an object such as:

const mySpecialColor = {
  50: '#F8FAFC',
  100: '#F1F5F9',
  200: '#E2E8F0',
  300: '#CBD5E1',
  400: '#94A3B8',
  500: '#64748B',
  600: '#475569',
  700: '#334155',
  800: '#1E293B',
  900: '#0F172A',
};

While it's not necessarily difficult to use various color generation tools to generate a palette based on a single base color, it can be tedious, especially if you are supplying multiple custom colors. Palette by Numbers, while not perfect, is intended to make that process easier. If we're being honest, this is just code I end up copying and pasting for reuse on each project, which is a bit silly.

Installation

# Using yarn
yarn add palette-by-numbers

# Using npm
npm install palette-by-numbers

Usage

Chakra UI

Create a utility function to extend the default Chakra UI theme:

import { extendTheme } from '@chakra-ui/react';
import { generatePalette } from 'palette-by-numbers';

export function createCustomTheme() {
  const colors = {
    primary: generatePalette('#61988E'),
    secondary: generatePalette('#493843'),
  };
  return extendTheme({ colors });
}
// In the above example, 'primary' would evaluate to:
// {
//   '50': 'hsla(170, 55%, 95%, 1)',
//   '100': 'hsla(169, 38%, 85%, 1)',
//   '200': 'hsla(169, 30%, 75%, 1)',
//   '300': 'hsla(169, 26%, 65%, 1)',
//   '400': 'hsla(169, 22%, 55%, 1)',
//   '500': '#61988E',
//   '600': 'hsla(169, 26%, 35%, 1)',
//   '700': 'hsla(169, 30%, 25%, 1)',
//   '800': 'hsla(169, 38%, 15%, 1)',
//   '900': 'hsla(170, 55%, 5%, 1)'
// }

Pass the theme prop to <ChakraProvider />:

import { Box, ChakraProvider } from '@chakra-ui/react';
import { createCustomTheme } from './util';

export const App = () => {
  const theme = createCustomTheme();
  // In practice, you could wrap this with useMemo().
  return (
    <ChakraProvider theme={theme}>
      <YourComponent />
    </ChakraProvider>
  );
};

const YourComponent = () => {
  return <Box color="primary.300">I have a special color</Box>;
};

Tailwind CSS

While I have not personally tested this with Tailwind, according to their docs, it should go something like this:

// tailwind.config.js
const { generatePalette } = require('palette-by-numbers');

module.exports = {
  theme: {
    extend: {
      colors: {
        primary: generatePalette('#61988E'),
        secondary: generatePalette('#493843'),
      },
    },
  },
};

By default, the midpoint, i.e. 500, is set to the original color passed. You can override this by passing an object as the second argument with the originalAtMidpoint set to false. For example:

const theme = generatePalette('#61988E', { originalAtMidpoint: false });
// This will generate:
// {
//   '50': 'hsla(170, 55%, 95%, 1)',
//   '100': 'hsla(169, 38%, 85%, 1)',
//   '200': 'hsla(169, 30%, 75%, 1)',
//   '300': 'hsla(169, 26%, 65%, 1)',
//   '400': 'hsla(169, 22%, 55%, 1)',
//   '500': 'hsla(169, 22%, 45%, 1)',
//   '600': 'hsla(169, 26%, 35%, 1)',
//   '700': 'hsla(169, 30%, 25%, 1)',
//   '800': 'hsla(169, 38%, 15%, 1)',
//   '900': 'hsla(170, 55%, 5%, 1)'
// }

Fonts

Also included is a font-family utility, which combines any passed font family with standard system fonts as fallbacks. For example:

import { extendTheme } from '@chakra-ui/react';
import { generateFontFamily } from 'palette-by-numbers';

export function createFontFamilies() {
  const fonts = {
    body: generateFontFamily('sans-serif', 'Open Sans'),
    mono: generateFontFamily('mono', 'Fira Code'),
    heading: generateFontFamily('serif', 'Merriweather'),
  };
  // Will generate:
  // {
  //   body: '"Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
  //   mono: '"Fira Code", SFMono-Regular, Melno, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
  //   heading: 'Merriweather, Garamond, "Palatino Linotype", "Book Antiqua", Palatino, "Times New Roman", Times, serif',
  // }
  return extendTheme({ fonts });
}