Skip to content

mrmendoza-dev/tailwind-color-mapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Tailwind Color Mapper

OKLCH-first Tailwind color mapping utilities with comprehensive format support

npm version License: MIT

A comprehensive TypeScript toolset and package for managing Tailwind CSS colors with OKLCH support. Build, validate, and consume consistent Tailwind color palettes with seamless format conversion.

๐Ÿ“ฆ Package Overview

๐Ÿ“Š Data Structure

The package includes comprehensive color data with 7 formats for each of the 242 color entries:

{
  "red": {
    "red-500": {
      "hex": "#fb2c36",
      "rgb": "rgb(251, 44, 54)",
      "hsl": "hsl(357, 96%, 58%)",
      "oklch": "oklch(0.637 0.237 25.331)",
      "rawRgb": [251, 44, 54],
      "rawHsl": [357, 96, 58],
      "rawOklch": [0.637, 0.237, 25.331]
    }
  }
}

๐Ÿš€ Quick Start

npm install tailwind-color-mapper
import {
  getColor,
  getColorShades,
  generateCSSVariables,
} from "tailwind-color-mapper";

// Get a specific color
const red500 = getColor("red", 500, "hex");
const blue500 = getColor("blue", 500, "oklch");

// Get all shades for a color
const redShades = getColorShades("red", "hex");

// Generate CSS variables
const cssVars = generateCSSVariables("blue", "primary");

Core Functions

  • getColor(colorName, colorCode, format) โ€“ Get specific color values
  • getColorShades(colorName, format) โ€“ Get all shades for a color
  • getAllColors(format) โ€“ Get all colors in a format
  • generateCSSVariables(colorName, prefix) โ€“ Generate CSS custom properties
  • generateTailwindConfig() โ€“ Generate Tailwind config object
  • findColorByHex(hexValue) โ€“ Search colors by hex value

Treeโ€‘Shakable Imports

// Import only what you need
import { getColor } from "tailwind-color-mapper/core";
import { getColor as getHex } from "tailwind-color-mapper/hex";
import { getColor as getOklch } from "tailwind-color-mapper/oklch";

๐ŸŽจ Supported Formats

  • hex โ€“ "#fb2c36"
  • rgb โ€“ "rgb(251, 44, 54)"
  • hsl โ€“ "hsl(357, 96%, 58%)"
  • oklch โ€“ "oklch(0.637 0.237 25.331)"
  • rawrgb โ€“ [251, 44, 54]
  • rawhsl โ€“ [357, 96, 58]
  • rawoklch โ€“ [0.637, 0.237, 25.331]
  • all โ€“ Complete color data object

๐ŸŒˆ Supported Colors

All 22 standard Tailwind colors with 11 shades each (50โ€‘950): red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose, slate, gray, zinc, neutral, stone.

๐Ÿ’ก Use Cases

import {
  generateCSSVariables,
  getColor,
  findColorByHex,
} from "tailwind-color-mapper";

// Design system variables
const primary = generateCSSVariables("blue", "primary");

// Color picker helpers
const selected = getColor("red", 500, "oklch");
const closest = findColorByHex("#ff6b6b");

๐Ÿ”ง TypeScript Support

import type {
  TailwindColor,
  TailwindShade,
  ColorFormat,
  ColorData,
} from "tailwind-color-mapper";

const color: string = getColor("red", 500, "hex");
const shades: Record<string, ColorData> = getColorShades("blue", "all");

๐ŸŒ CDN Usage

<script type="module">
  import { getColor } from "https://esm.sh/tailwind-color-mapper@latest";
  console.log(getColor("red", 500, "hex"));
</script>

๐Ÿ“Š Package Size

  • Core: ~20KB
  • Hex: ~15KB
  • OKLCH: ~20KB
  • Full: ~86KB

๐ŸŽฏ Key Features

  • โœ… OKLCHโ€‘first, modern color space support
  • โœ… Typeโ€‘safe, full TypeScript
  • โœ… Treeโ€‘shakable, import only what you need
  • โœ… Validated data (242 entries)
  • โœ… Multiple formats: HEX, RGB, HSL, OKLCH
  • โœ… CDN ready

๐Ÿงช Generator & Tools

This repo also includes a generator and maintenance tools for producing and validating tailwindMapper.json.

generator/tailwind-mapper-generator.ts

Interactive CLI to create a fresh color dataset from OKLCH values.

  • Prompts for all 22 Tailwind colors with 11 shades each
  • Converts OKLCH โ†’ RGB, HSL, HEX
  • Saves progress after each entry
  • Fully typed, with validation

Run:

npx tsx generator/tailwind-mapper-generator.ts

generator/tailwind-mapper-functions.ts

Userโ€‘facing utilities for consuming the color data in apps and tooling.

  • getColor(colorName, colorCode, format)
  • getColorShades(colorName, format)
  • getAllColors(format)
  • generateCSSVariables(colorName, prefix)
  • generateTailwindConfig()
  • findColorByHex(hexValue)

Supports flexible colorCode input ("red-500" or 500) and exports VALID_COLORS, VALID_SHADES, VALID_FORMATS.

generator/tailwind-mapper-validation.ts

Validation and maintenance helpers for the color dataset.

  • validateMapper() โ€“ Validate color data
  • fixMapperIssues(dryRun) โ€“ Fix common issues
  • regenerateAllColors() โ€“ Regenerate from OKLCH

Run:

# Validate
npx tsx -e "import('./generator/tailwind-mapper-validation.ts').then(m => m.validateMapper())"

# Dryโ€‘run fixes
npx tsx -e "import('./generator/tailwind-mapper-validation.ts').then(m => m.fixMapperIssues(true))"

# Regenerate from OKLCH
npx tsx generator/tailwind-mapper-validation.ts

Type Definitions (Generator)

type TailwindColor =
  | "red"
  | "orange"
  | "amber"
  | "yellow"
  | "lime"
  | "green"
  | "emerald"
  | "teal"
  | "cyan"
  | "sky"
  | "blue"
  | "indigo"
  | "violet"
  | "purple"
  | "fuchsia"
  | "pink"
  | "rose"
  | "slate"
  | "gray"
  | "zinc"
  | "neutral"
  | "stone";

type TailwindShade =
  | 50
  | 100
  | 200
  | 300
  | 400
  | 500
  | 600
  | 700
  | 800
  | 900
  | 950;
type ColorFormat =
  | "hex"
  | "rgb"
  | "hsl"
  | "oklch"
  | "rawrgb"
  | "rawhsl"
  | "rawoklch"
  | "all";
type ColorCode = string | number;

interface ColorData {
  hex: string;
  rgb: string;
  hsl: string;
  oklch: string;
  rawRgb: [number, number, number];
  rawHsl: [number, number, number];
  rawOklch: [number, number, number];
}

๐Ÿ“š Examples

import React from "react";
import { getColor, getColorShades } from "tailwind-color-mapper";

export function ColorSwatch({ colorName }: { colorName: string }) {
  const shades = getColorShades(colorName, "hex");
  return (
    <div className="flex gap-2">
      {Object.entries(shades).map(([shade, hex]) => (
        <div
          key={shade}
          className="w-8 h-8 rounded"
          style={{ backgroundColor: hex }}
        />
      ))}
    </div>
  );
}

๐Ÿค Contributing

Please see CONTRIBUTING.md for guidelines.

๐Ÿ“„ License

MIT License โ€“ see LICENSE.

๐Ÿ”— Links

About

Npm package for easily retrieving and mapping Tailwind CSS colors. Supports OKCLH, RGB, HEX, and HSL.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published