Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename colorspace() to vertexColorSpace() #899

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
export * from './center.js';
export * from './clear-node-parent.js';
export * from './clear-node-transform.js';
export * from './colorspace.js';
export * from './dedup.js';
export * from './dequantize.js';
export * from './draco.js';
Expand Down Expand Up @@ -88,4 +87,5 @@ export * from './unlit.js';
export * from './unpartition.js';
export { getGLPrimitiveCount, isTransformPending, createTransform } from './utils.js';
export * from './unweld.js';
export * from './vertex-color-space.js';
export * from './weld.js';
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
import type { Accessor, Document, Primitive, Transform, vec3 } from '@gltf-transform/core';
import { createTransform } from './utils.js';

const NAME = 'colorspace';
const NAME = 'vertexColorSpace';

/** Options for the {@link colorspace} function. */
export interface ColorspaceOptions {
/** Must be `"sRGB"`. Required. */
inputEncoding: string;
/** Options for the {@link vertexColorSpace} function. */
export interface ColorSpaceOptions {
/** Input color space of vertex colors, to be converted to "srgb-linear". Required. */
inputColorSpace: 'srgb' | 'srgb-linear' | 'sRGB';
/** @deprecated Renamed to 'colorSpace'. */
inputEncoding?: 'srgb' | 'srgb-linear' | 'sRGB';
}

/** @deprecated Renamed to {@link vertexColorSpace}. */
export const colorspace = vertexColorSpace;

/**
* Vertex color colorspace correction. The glTF format requires vertex colors to be stored
* as linear values, and this function provides a way to correct vertex colors that are
* (incorrectly) sRGB.
* Vertex color color space correction. The glTF format requires vertex colors to be stored
* in Linear Rec. 709 D65 color space, and this function provides a way to correct vertex
* colors that are (incorrectly) stored in sRGB.
*
* Example:
*
* ```typescript
* import { vertexColorSpace } from '@gltf-transform/functions';
*
* await document.transform(
* vertexColorspace({ inputColorSpace: 'srgb' })
* );
* ```
*/
export function colorspace(options: ColorspaceOptions): Transform {
export function vertexColorSpace(options: ColorSpaceOptions): Transform {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();

if (options.inputEncoding === 'linear') {
const inputColorSpace = (options.inputColorSpace || options.inputEncoding || '').toLowerCase();

if (inputColorSpace === 'srgb-linear') {
logger.info(`${NAME}: Vertex colors already linear. Skipping conversion.`);
return;
}

if (options.inputEncoding !== 'sRGB') {
if (inputColorSpace !== 'srgb') {
logger.error(
`${NAME}: Unknown input encoding "${options.inputEncoding}" – should be "sRGB" or ` +
'"linear". Skipping conversion.'
`${NAME}: Unknown input color space "${inputColorSpace}" – should be "srgb" or ` +
'"srgb-linear". Skipping conversion.'
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import { Accessor, Document } from '@gltf-transform/core';
import { colorspace } from '@gltf-transform/functions';
import { vertexColorSpace } from '@gltf-transform/functions';

test('basic', (t) => {
const input = [0.25882352941176473, 0.5215686274509804, 0.9568627450980393]; // sRGB
Expand All @@ -22,7 +22,7 @@ test('basic', (t) => {
primitive1.setAttribute('COLOR_0', accessor1).setAttribute('COLOR_1', accessor2);
primitive2.setAttribute('COLOR_0', accessor1);

colorspace({ inputEncoding: 'sRGB' })(doc);
vertexColorSpace({ inputColorSpace: 'srgb' })(doc);

let actual;

Expand Down