Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /