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 3a15c36 commit a84ef6b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,31 @@ simple lookup by array index.
| ---------- | ---------- |
| `intensityToTurbo` | `(value: number) => Color` |

Parameters:

* `value`: An integer in the range 0-255.


### :gear: snapColorToTurbo

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.

For performance, this uses a pre-initialized k-d tree to perform
nearest-neighbor search.
For performance, this uses a pre-initialized k-d tree to perform nearest-neighbor
search.

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

Parameters:

* `rgbColor`: An array-like RGB triplet.
* `cache`: A Map to use as a lookup cache, to avoid repeated nearest-neighbor
searches.


### :gear: snapColorToIntensity

Accepts an arbitrary RGB triplet and returns the index (0-255) of the nearest
Expand All @@ -115,8 +127,7 @@ Parameters:

* `rgbColor`: An array-like RGB triplet.
* `cache`: A Map to use as a lookup cache, to avoid repeated nearest-neighbor
searches. If not provided, a temporary one will be used for each function
call.
searches.


### :gear: snapNormalizedToTurbo
Expand Down
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ const rgbTree = new KDTree(rgbColormap, 3);
/**
* Convert an integer in the range 0-255 to a Turbo RGB triplet. This is a
* simple lookup by array index.
*
* @param value An integer in the range 0-255.
*
* @returns A Uint8ClampedArray Turbo RGB triplet.
*/
export function intensityToTurbo(value: number): Color {
return rgbColormap[value];
Expand All @@ -145,8 +149,14 @@ export function intensityToTurbo(value: number): Color {
* distance) in the Turbo colormap. There is no interpolation; one of the 256
* colors in the exact Turbo palette is always returned.
*
* For performance, this uses a pre-initialized k-d tree to perform
* nearest-neighbor search.
* For performance, this uses a pre-initialized k-d tree to perform nearest-neighbor
* search.
*
* @param rgbColor An array-like RGB triplet.
* @param cache A Map to use as a lookup cache, to avoid repeated nearest-neighbor
* searches.
*
* @returns A Turbo RGB triplet in a Uint8ClampedArray.
*/
export function snapColorToTurbo(rgbColor: Color, cache?: Map<string, number>) {
const index = snapColorToIntensity(rgbColor, cache);
Expand All @@ -160,8 +170,9 @@ export function snapColorToTurbo(rgbColor: Color, cache?: Map<string, number>) {
*
* @param rgbColor An array-like RGB triplet.
* @param cache A Map to use as a lookup cache, to avoid repeated nearest-neighbor
* searches. If not provided, a temporary one will be used for each function
* call.
* searches.
*
* @returns An integer in the range 0-255.
*/
export function snapColorToIntensity(
rgbColor: Color,
Expand Down Expand Up @@ -199,6 +210,8 @@ export function snapNormalizedToTurbo(value: number): Color {
* Turbo, a new in-between color is generated via simple Euclidian interpolation.
*
* @param value A number in the range 0-1.
*
* @returns A Turbo RGB triplet in a standard JavaScript Array.
*/
export function interpolateNormalizedToTurbo(value: number): Color {
const a = Math.floor(value * 255);
Expand All @@ -223,6 +236,8 @@ export function interpolateNormalizedToTurbo(value: number): Color {
* averaged.
*
* @param gray An array-like RGB triplet.
*
* @returns A Turbo RGB triplet in a Uint8ClampedArray.
*/
export function grayscaleToTurbo(gray: Color) {
const r = gray[0];
Expand Down

0 comments on commit a84ef6b

Please sign in to comment.