@luma.gl/shadertools
Version:
Shader module system for luma.gl
42 lines • 1.73 kB
JavaScript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/**
* Resolves whether semantic colors should be interpreted as byte-style `0..255` values.
* @param useByteColors - Explicit color interpretation flag.
* @param defaultUseByteColors - Fallback value when `useByteColors` is omitted.
* @returns `true` when semantic colors should be normalized from bytes, otherwise `false`.
*/
export function resolveUseByteColors(useByteColors, defaultUseByteColors = true) {
return useByteColors ?? defaultUseByteColors;
}
/**
* Normalizes an RGB semantic color to float space when byte-style colors are enabled.
* @param color - Input RGB semantic color.
* @param useByteColors - When `true`, divide components by `255`.
* @returns The normalized RGB color.
*/
export function normalizeByteColor3(color = [0, 0, 0], useByteColors = true) {
if (!useByteColors) {
return [...color];
}
return color.map(component => component / 255);
}
/**
* Normalizes an RGBA semantic color to float space when byte-style colors are enabled.
* @param color - Input RGB or RGBA semantic color.
* @param useByteColors - When `true`, divide components by `255`.
* @returns The normalized RGBA color, adding an opaque alpha channel when needed.
*/
export function normalizeByteColor4(color, useByteColors = true) {
const normalizedColor = normalizeByteColor3(color.slice(0, 3), useByteColors);
const hasAlpha = Number.isFinite(color[3]);
const alpha = hasAlpha ? color[3] : 1;
return [
normalizedColor[0],
normalizedColor[1],
normalizedColor[2],
useByteColors && hasAlpha ? alpha / 255 : alpha
];
}
//# sourceMappingURL=normalize-byte-colors.js.map