Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
exogen committed Oct 10, 2024
1 parent 69bc6b2 commit 8d56f83
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 69 deletions.
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,72 +50,73 @@ This library has helpers for performing these tasks and more.

## :toolbox: Functions

- [quantizeRGB](#gear-quantizergb)
- [rgbToIntensity](#gear-rgbtointensity)
- [interpolateNormalizedToRGB](#gear-interpolatenormalizedtorgb)
- [snapNormalizedToRGB](#gear-snapnormalizedtorgb)
- [intensityToRGB](#gear-intensitytorgb)
- [grayscaleToRGB](#gear-grayscaletorgb)
- [snapToTurbo](#gear-snaptoturbo)
- [snapToIntensity](#gear-snaptointensity)
- [snapNormalizedToRGB](#gear-snapnormalizedtorgb)
- [interpolateNormalizedToRGB](#gear-interpolatenormalizedtorgb)
- [grayscaleToTurbo](#gear-grayscaletoturbo)
- [convertTurboBufferToGrayscale](#gear-convertturbobuffertograyscale)
- [convertColorBufferToTurbo](#gear-convertcolorbuffertoturbo)
- [convertGrayscaleBufferToTurbo](#gear-convertgrayscalebuffertoturbo)

### :gear: quantizeRGB
### :gear: intensityToRGB

Accepts an arbitrary RGB triplet and returns the nearest (Euclidian distance)
color in the Turbo colormap. There is no interpolation; one of the 256 colors
used to define the Turbo palette is always returned.
Convert an integer in the range 0-255 to a Turbo RGB triplet. This is a
simple lookup by array index.

| Function | Type |
| ---------- | ---------- |
| `quantizeRGB` | `(rgbColor: Color, cache?: Map<string, number> or undefined) => Uint8ClampedArray` |
| `intensityToRGB` | `(value: number) => Color` |

### :gear: rgbToIntensity
### :gear: snapToTurbo

Accepts an arbitrary RGB triplet and returns an intensity value (0-255). The
intensity can either be used directly as a grayscale value, or as an index
into the Turbo colormap.
Accepts an arbitrary RGB triplet and returns the nearest color (by Euclidian
distance) in the Turbo colormap. There is no interpolation; one of the 256
colors in the exact Turbo palette is always returned.

| Function | Type |
| ---------- | ---------- |
| `rgbToIntensity` | `(rgbColor: Color, cache?: Map<string, number> or undefined) => number` |
| `snapToTurbo` | `(rgbColor: Color, cache?: Map<string, number> or undefined) => Uint8ClampedArray` |

### :gear: interpolateNormalizedToRGB
### :gear: snapToIntensity

Accepts a float in the range 0-1 and returns an interpolated Turbo color.
That is, if the intensity lies between two of the 256 color stops defined by
Turbo, a new color is generated via simple Euclidian interpolation.
Accepts an arbitrary RGB triplet and returns an intensity value (0-255). The
intensity can either be used directly as a grayscale value, or as an index
into the Turbo colormap.

| Function | Type |
| ---------- | ---------- |
| `interpolateNormalizedToRGB` | `(value: number) => Color` |
| `snapToIntensity` | `(rgbColor: Color, cache?: Map<string, number> or undefined) => number` |

### :gear: snapNormalizedToRGB

Accepts a float in the range 0-1 and returns the nearest color in the
quantized Turbo palette.
indexed Turbo palette.

| Function | Type |
| ---------- | ---------- |
| `snapNormalizedToRGB` | `(value: number) => Color` |

### :gear: intensityToRGB
### :gear: interpolateNormalizedToRGB

Convert an integer in the range 0-255 to a Turbo RGB triplet.
Accepts a float in the range 0-1 and returns an interpolated Turbo color.
That is, if the value lies between two of the 256 indexed colors defined by
Turbo, a new in-between color is generated via simple Euclidian interpolation.

| Function | Type |
| ---------- | ---------- |
| `intensityToRGB` | `(value: number) => Color` |
| `interpolateNormalizedToRGB` | `(value: number) => Color` |

### :gear: grayscaleToRGB
### :gear: grayscaleToTurbo

Convert a gray RGB triplet to a Turbo RGB triplet. If the color is not
perfectly gray (same value for R, G, B) then their intensities will be
averaged.

| Function | Type |
| ---------- | ---------- |
| `grayscaleToRGB` | `(gray: Color) => Color` |
| `grayscaleToTurbo` | `(gray: Color) => Color` |

### :gear: convertTurboBufferToGrayscale

Expand Down
4 changes: 2 additions & 2 deletions demo/app/components.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { useEffect, useMemo, useRef, useState } from "react";
import { intensityToRGB, quantizeRGB } from "../..";
import { intensityToRGB, snapToTurbo } from "../..";
import ColorSlider from "../src/ColorSlider";
import { ColorPicker, useColor } from "react-color-palette";
import "react-color-palette/css";
Expand Down Expand Up @@ -105,7 +105,7 @@ export function Quantizer() {
Math.round(color.rgb.g),
Math.round(color.rgb.b),
];
return quantizeRGB([r, g, b]);
return snapToTurbo([r, g, b]);
}, [color]);

return (
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/404/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/index.txt

Large diffs are not rendered by default.

69 changes: 35 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,20 @@ for (let i = 0; i < rgbColormap.length; i++) {
const rgbTree = new KDTree(rgbColormap, 3);

/**
* Accepts an arbitrary RGB triplet and returns the nearest (Euclidian distance)
* color in the Turbo colormap. There is no interpolation; one of the 256 colors
* used to define the Turbo palette is always returned.
* Convert an integer in the range 0-255 to a Turbo RGB triplet. This is a
* simple lookup by array index.
*/
export function quantizeRGB(rgbColor: Color, cache?: Map<string, number>) {
const index = rgbToIntensity(rgbColor, cache);
export function intensityToRGB(value: number): Color {
return rgbColormap[value];
}

/**
* Accepts an arbitrary RGB triplet and returns the nearest color (by Euclidian
* distance) in the Turbo colormap. There is no interpolation; one of the 256
* colors in the exact Turbo palette is always returned.
*/
export function snapToTurbo(rgbColor: Color, cache?: Map<string, number>) {
const index = snapToIntensity(rgbColor, cache);
return rgbColormap[index];
}

Expand All @@ -147,7 +155,7 @@ export function quantizeRGB(rgbColor: Color, cache?: Map<string, number>) {
* intensity can either be used directly as a grayscale value, or as an index
* into the Turbo colormap.
*/
export function rgbToIntensity(
export function snapToIntensity(
rgbColor: Color,
cache?: Map<string, number>
): number {
Expand All @@ -167,10 +175,18 @@ export function rgbToIntensity(
return bestValue as number;
}

/**
* Accepts a float in the range 0-1 and returns the nearest color in the
* indexed Turbo palette.
*/
export function snapNormalizedToRGB(value: number): Color {
return intensityToRGB(normalizedToIntensity(value));
}

/**
* Accepts a float in the range 0-1 and returns an interpolated Turbo color.
* That is, if the intensity lies between two of the 256 color stops defined by
* Turbo, a new color is generated via simple Euclidian interpolation.
* That is, if the value lies between two of the 256 indexed colors defined by
* Turbo, a new in-between color is generated via simple Euclidian interpolation.
*/
export function interpolateNormalizedToRGB(value: number): Color {
const a = Math.floor(value * 255);
Expand All @@ -189,34 +205,12 @@ export function interpolateNormalizedToRGB(value: number): Color {
];
}

/**
* Accepts a float in the range 0-1 and returns the nearest color in the
* quantized Turbo palette.
*/
export function snapNormalizedToRGB(value: number): Color {
return intensityToRGB(normalizedToIntensity(value));
}

/**
* Convert a float in the range 0-1 to an integer in the range 0-255.
*/
function normalizedToIntensity(value: number): number {
return Math.floor(value * 255);
}

/**
* Convert an integer in the range 0-255 to a Turbo RGB triplet.
*/
export function intensityToRGB(value: number): Color {
return rgbColormap[value];
}

/**
* Convert a gray RGB triplet to a Turbo RGB triplet. If the color is not
* perfectly gray (same value for R, G, B) then their intensities will be
* averaged.
*/
export function grayscaleToRGB(gray: Color) {
export function grayscaleToTurbo(gray: Color) {
const r = gray[0];
const g = gray[1];
const b = gray[2];
Expand All @@ -228,6 +222,13 @@ export function grayscaleToRGB(gray: Color) {
}
}

/**
* Convert a float in the range 0-1 to an integer in the range 0-255.
*/
function normalizedToIntensity(value: number): number {
return Math.floor(value * 255);
}

/**
* Given an ArrayBuffer-like `buffer` containing RGBA intensities, return a new
* ArrayBuffer (or the provided `targetBuffer`) with the RGB pixel values
Expand All @@ -243,7 +244,7 @@ export function convertTurboBufferToGrayscale(
const targetArray = new Uint8ClampedArray(targetBuffer);
for (let i = 0; i < len; i += 4) {
const color = new Uint8ClampedArray(buffer, i, 4);
const intensity = rgbToIntensity(color, cache);
const intensity = snapToIntensity(color, cache);
targetArray[i] = intensity;
targetArray[i + 1] = intensity;
targetArray[i + 2] = intensity;
Expand All @@ -265,7 +266,7 @@ export function convertColorBufferToTurbo(
const targetArray = new Uint8ClampedArray(targetBuffer);
for (let i = 0; i < len; i += 4) {
const color = new Uint8ClampedArray(buffer, i, 4);
const turboColor = grayscaleToRGB(color);
const turboColor = snapToTurbo(color);
targetArray[i] = turboColor[0];
targetArray[i + 1] = turboColor[1];
targetArray[i + 2] = turboColor[2];
Expand All @@ -287,7 +288,7 @@ export function convertGrayscaleBufferToTurbo(
const targetArray = new Uint8ClampedArray(targetBuffer);
for (let i = 0; i < len; i += 4) {
const color = new Uint8ClampedArray(buffer, i, 4);
const turboColor = grayscaleToRGB(color);
const turboColor = grayscaleToTurbo(color);
targetArray[i] = turboColor[0];
targetArray[i + 1] = turboColor[1];
targetArray[i + 2] = turboColor[2];
Expand Down

0 comments on commit 8d56f83

Please sign in to comment.