Skip to content

Repository files navigation

beer-color-rgb

Convert EBC (European Brewery Convention) or SRM (Standard Reference Method) beer color values to RGB hex codes with scientific accuracy. Zero dependencies, fully typed, and ultra-lightweight (~2kB bundle size).

npm Bundle Size Types Build Socket Badge Node.js License

Table of Contents

What is it

beer-color-rgb takes an EBC or SRM value and converts it to a precise RGB color. It uses the A.J. de Lange spectral model — a scientifically validated method based on light transmission analysis of 99 real beers — producing colorimetrically accurate results suitable for brewing apps, color pickers, and design systems.

One package to rule them all, One library to type them, One plugin to style them all, Or in the CLI compile them.

Choose how you want to integrate it into your workflow:

  • As a core TypeScript library for direct color conversion logic.
  • As a Tailwind CSS v4 plugin to auto-generate color utility classes.
  • As a CLI tool to generate static JSON/CSS color palettes.

→ Live demo

Getting Started

Prerequisites:

npm

Install

npm install beer-color-rgb

Usage

As a library

import { ebcToHex, ebcToRgb, ebcToRgbObject, srmToHex } from "beer-color-rgb";

// EBC
ebcToHex(20); // → "#b95900"
ebcToRgb(20); // → "rgb(185, 89, 0)"
ebcToRgbObject(20); // → { r: 185, g: 89, b: 0 }

// SRM
srmToHex(10); // → "#ba5b00"

// Custom optical path (cm)
ebcToHex(20, { lightPath: 3 }); // → "#d88900"

As a CLI

# Single EBC conversion
npx beer-color-rgb 20
# → #b95900

# Single SRM conversion
npx beer-color-rgb --srm 10
# → #ba5b00

# Batch CSS generation
npx beer-color-rgb generate --format css --unit ebc --output colors.css

# Batch JSON generation
npx beer-color-rgb generate --format json --unit srm --output colors.json

# Custom optical path
npx beer-color-rgb 20 --path 3
# → #d88900

As a Tailwind CSS plugin

Tailwind CSS v4 (Recommended):

@import "tailwindcss";
@plugin "beer-color-rgb/plugin";

/* Optional: configure via @theme */
@theme {
  --beer-light-path: 3; /* optical path in cm, default 5 */
  --beer-ebc-start: 1; /* EBC range start, default 1 */
  --beer-ebc-end: 20; /* EBC range end, default 80 */
  --beer-srm-start: 1; /* SRM range start, default 1 */
  --beer-srm-end: 15; /* SRM range end, default 40 */
}

Tailwind CSS v3 (Legacy):

// tailwind.config.ts
import { beerColorPlugin } from "beer-color-rgb/plugin";

export default {
  plugins: [beerColorPlugin()],
};

This generates utility classes for EBC 1–80 and SRM 1–40:

<div class="ebc-bg-20">...</div>
<div class="srm-bg-10">...</div>

<!-- Decimal values supported, though uncommon in practice -->
<div class="ebc-bg-[35.5]">...</div>

Plugin options (v3 JS config — all options available):

beerColorPlugin({
  ebcRange: [1, 80], // or false to disable
  srmRange: [1, 40], // or false to disable
  lightPath: 5, // optical path in cm (default: 5)
});

Note: Disabling a range entirely (false) is v3/JS-only. In v4, use @theme variables to restrict the range.

Project Structure

beer-color-rgb/
├── src/
│   ├── convert.ts          # Core conversion logic (A.J. de Lange spectral model)
│   ├── index.ts            # Public API exports
│   ├── cli.ts              # Command-line interface
│   └── plugin.ts           # Tailwind CSS plugin
├── tests/
│   ├── convert.test.ts
│   ├── index.test.ts
│   └── plugin.test.ts
├── package.json
├── tsconfig.json
└── CHANGELOG.md

Configuration

Environment variables

None required. The conversion uses fixed CIE 1931 colorimetric data and D65 illuminant (standard daylight).

API reference

EBC functions

ebcToHex(ebc: number, options?: ColorOptions): string
// → "#b95900"

ebcToRgb(ebc: number, options?: ColorOptions): string
// → "rgb(185, 89, 0)"

ebcToRgbObject(ebc: number, options?: ColorOptions): { r: number; g: number; b: number }
// → { r: 185, g: 89, b: 0 }

ebcToRgbArray(ebc: number, options?: ColorOptions): [number, number, number]
// → [185, 89, 0]

SRM functions (SRM × 1.97 = EBC internally)

srmToHex(srm: number, options?: ColorOptions): string
srmToRgb(srm: number, options?: ColorOptions): string
srmToRgbObject(srm: number, options?: ColorOptions): { r: number; g: number; b: number }
srmToRgbArray(srm: number, options?: ColorOptions): [number, number, number]

ColorOptions

type ColorOptions = {
  lightPath?: number; // Optical path in cm (default: 5.0)
};
lightPath Use case
5.0 (default) BJCP standard — typical glass viewed in daylight
3.0 Aesthetic middle ground — richer apparent color
1.27 ASBC/EBC laboratory measurement standard

How it works

The A.J. de Lange spectral model reconstructs the light transmission spectrum for a given EBC value, then converts the result to sRGB using CIE 1931 colorimetry with D65 (6500K daylight) illumination.

Pipeline:

EBC → Spectral transmission T(λ) → XYZ tristimulus → sRGB linear → sRGB gamma correction → #RRGGBB

This approach is more accurate than polynomial or exponential approximations, especially for pale beers (EBC 1–20) and dark beers (EBC 50+).

References

License

MIT — see LICENSE file.

About

Convert EBC (European Brewery Convention) or SRM (Standard Reference Method) beer color values to RGB hex codes with scientific accuracy.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages