# color2k: Size

## Why color2k

You have a brand color. You need its hover state, a faded version for the focus ring, and to know whether the text on top should be black or white. That is most of what an app does with color, and color2k does it in 2.5 kB.

- **It's small.** All 25 exports come to 2.5 kB. Import only `darken` and you ship 1.6 kB.
- **It's just functions.** Pass a CSS color string and get one back. There is nothing to set up or chain.
- **It covers the daily work.** Parse and convert colors, lighten or darken them, mix them, build scales and check WCAG contrast.
- **It's boring to depend on.** No dependencies, a test file for every function, TypeScript types and npm provenance. The build still runs on Node 6.

### What it costs

All 25 exports together are **2.48 kB**, minified and gzipped. Import one and this is what lands in your bundle:

| Import | Alone | Import | Alone | Import | Alone |
| --- | --- | --- | --- | --- | --- |
| `getScale` | 1.70 kB | `mix` | 1.63 kB | `transparentize` | 1.53 kB |
| `complement` | 1.69 kB | `getContrast` | 1.58 kB | `toRgba` | 1.52 kB |
| `lighten` | 1.68 kB | `invert` | 1.58 kB | `parseToRgba` | 1.46 kB |
| `saturate` | 1.68 kB | `parseToHsla` | 1.58 kB | `hsla` | 312 B |
| `desaturate` | 1.67 kB | `readableColor` | 1.58 kB | `rgba` | 283 B |
| `adjustHue` | 1.67 kB | `readableColorIsBlack` | 1.57 kB | `ColorError` | 249 B |
| `darken` | 1.67 kB | `getLuminance` | 1.55 kB | `guard` | 227 B |
| `toHsla` | 1.66 kB | `opacify` | 1.54 kB |  |  |
| `hasBadContrast` | 1.65 kB | `toHex` | 1.54 kB |  |  |

The parser is about 1.5 kB and nearly every function needs it. So the first import costs the most, and each one after it adds very little. `scripts/sizes.js` measures these with Rollup, Terser and gzip, and CI fails if they change without the table.

### Next to the others

| Library | Size (min + gzip) |
| --- | --- |
| [chroma-js](https://github.com/gka/chroma.js) | [16.7 kB](https://bundlephobia.com/package/chroma-js) |
| [polished](https://github.com/styled-components/polished) | [11.6 kB](https://bundlephobia.com/package/polished) |
| [color](https://github.com/Qix-/color) | [8.5 kB](https://bundlephobia.com/package/color) |
| [tinycolor2](https://github.com/bgrins/TinyColor) | [5.4 kB](https://bundlephobia.com/package/tinycolor2) |
| [colord](https://github.com/omgovich/colord) with the names, mix and a11y plugins | 4.7 to 5.0 kB |
| **color2k** | [**2.5 kB**](https://bundlephobia.com/package/color2k) |

The single figures come from Bundlephobia, November 2026. colord's core is 2.3 kB without named colors, mixing or contrast checks, so its row is measured with the plugins that cover what color2k does. Bundled the way `scripts/sizes.js` measures color2k (Rollup, Terser with two passes, gzip at level 9), colord 2.14.0 with those three plugins is 4.86 kB. Other minifiers put it between 4.7 and 5.0 kB.

### An alternative to TinyColor and Polished

color2k covers the everyday calls of both. Three things change when you move over. You import functions by name, the color comes first, and amounts are decimals from 0 to 1.

In [tinycolor2](https://github.com/bgrins/TinyColor), amounts run from 0 to 100 and calls chain off an object:

```js
import { darken, getContrast, mix, toHex } from 'color2k';

// tinycolor('#0d6efd').darken(10).toHexString()
toHex(darken('#0d6efd', 0.1)); // '#0256d4'

// tinycolor.mix('gold', 'tomato', 25).toHexString()
toHex(mix('gold', 'tomato', 0.25)); // '#ffba12'

// tinycolor.readability('#0d6efd', '#fff')
getContrast('#0d6efd', '#fff'); // 4.500782787406787
```

In [polished](https://github.com/styled-components/polished), the amount comes first and the color last:

```js
import { darken, mix, transparentize } from 'color2k';

// darken(0.1, '#0d6efd')
darken('#0d6efd', 0.1); // 'hsla(216, 98%, 42%, 1)'

// mix(0.75, 'gold', 'tomato')
// In polished the weight is the share of the first color. Here it is the second.
mix('gold', 'tomato', 0.25); // 'rgba(255, 186, 18, 1)'

// transparentize(0.5, '#0d6efd')
transparentize('#0d6efd', 0.5); // 'rgba(13, 110, 253, 0.5)'
```

Both of them return hex for a solid color. color2k returns `hsla()` and `rgba()` strings, so wrap a result in `toHex` when you need hex. HSL results are rounded to whole percents, so that hex can sit one step away from theirs in a channel.

## How it stays small

color2k is boring on purpose. Each function takes a string and returns a string. There is nothing to configure and nothing to chain. The one clever thing in here is the packed table of color names, which costs 561 bytes. The rest is plain code written with care, so you can drop it into anything.

- **It does less.** color2k has the color functions apps use every day, in sRGB, and stops there.
- **Two output formats.** Results come back as `rgba()` or `hsla()` strings, which every browser reads, and `toHex` covers the rest. That skips the code other libraries spend on optional alpha channels and extra color models.
- **A squeezed color-name table.** The 148 CSS color names are stored as one hashed string of 740 characters. A plain lookup object would be several times that.
- **One function per file.** Nothing is shared but the parser, so your bundler drops whatever you don't import.

Missing something you need? [Open an issue](https://github.com/ricokahler/color2k/issues/new/choose).
