Skip to content

Commit eedb24f

Browse files
feat(pixel): add FLOAT_GRAY_RANGE format
1 parent ddf0980 commit eedb24f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { fit, fitClamped } from "@thi.ng/math/fit";
2+
import { mix } from "@thi.ng/math/mix";
3+
import { FloatFormat, Lane } from "../api.js";
4+
import { __luminanceABGR } from "../internal/utils.js";
5+
6+
/**
7+
* Higher order, single channel float pixel format using provided [min..max]
8+
* value range, instead of usual [0..1] interval.
9+
*
10+
* @param min
11+
* @param max
12+
*/
13+
export const FLOAT_GRAY_RANGE = (min: number, max: number): FloatFormat => {
14+
return {
15+
__float: true,
16+
alpha: false,
17+
gray: true,
18+
channels: [Lane.RED],
19+
shift: { 3: 0 },
20+
size: 1,
21+
getNormalized: (val) => {
22+
return fitClamped(val, min, max, 0, 1);
23+
},
24+
fromABGR: (src) => [mix(min, max, __luminanceABGR(src) / 255)],
25+
toABGR: (src) =>
26+
((fit(src[0], min, max, 0, 255) | 0) * 0x010101) | 0xff000000,
27+
};
28+
};

packages/pixel/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * from "./format/rgb888.js";
2727
export * from "./format/float-format.js";
2828
export * from "./format/float-gray.js";
2929
export * from "./format/float-gray-alpha.js";
30+
export * from "./format/float-gray-range.js";
3031
export * from "./format/float-hsva.js";
3132
export * from "./format/float-norm.js";
3233
export * from "./format/float-rgb.js";

0 commit comments

Comments
 (0)