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

Add support for THREE.ColorManagement #123

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ gui.addColor( obj, 'colorObject', 255 );
gui.addColor( obj, 'colorArray', 255 );
```

### Custom Color Spaces

lil-gui allows you to work with colors outside of the typical sRGB color space, so long as they provide conversion methods to and from sRGB hex integers.

```js
class MyColor {

// convert this color to hex int (0xff00aa)
getHex() { return hexInt; }

// parse int and convert to this color space
setHex( hexInt ) {}

}
```

If present, lil-gui will use these methods instead of reading or writing the `{ r, g, b }` components directly.

## Folders

You can organize controllers in collapsible groups using `addFolder()`. The method returns a new GUI
Expand Down
2 changes: 2 additions & 0 deletions Migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ params = { color };
lil_gui.addColor( params, 'color' );
```

_Note: lil-gui is automatically converting color spaces under the hood since THREE.Color provides getHex/setHex methods._
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part of the Migrating section talks about how lil-gui allows you to forego the old onChange pattern from dat.gui. i'm thinking about changing this example to use a different lib with 0-1 RGB values (maybe PIXI?). this way we don't have to gloss over colorspaces.


The other differences in color handling are fairly minor:

- lil-gui always writes to `#rrggbb` format for strings, even those defined as `rgb()` or `#RGB`.
Expand Down
36 changes: 36 additions & 0 deletions examples/custom-color/custom-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as THREE from 'https://cdn.jsdelivr.net/npm/three/build/three.module.js';
import GUI from '../../dist/lil-gui.esm.js';

const debugDiv = document.getElementById( 'debug-div' );

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 2;

const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00aaff } );
const cube = new THREE.Mesh( geometry, material );

scene.add( cube );
const gui = new GUI();
const ctrl = gui.addColor( material, 'color' );

function animate() {

requestAnimationFrame( animate );

cube.rotation.x += 0.01;
cube.rotation.y += 0.01;

debugDiv.style.backgroundColor = '#' + ctrl.$text.value;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to delete this example before we merge, but here's what i was really after...


renderer.render( scene, camera );

}

animate();
28 changes: 28 additions & 0 deletions examples/custom-color/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
}
canvas {
display: block;
}
#debug-div {
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 100%;
}

</style>
</head>
<body>
<div id="debug-div"></div>
<script type="module" src="custom-color.js"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions homepage/guide-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ example( 12, ( gui, code ) => {

} );

example( 13, gui => {
example( 14, gui => {

const params = {
scale: 1,
Expand All @@ -139,7 +139,7 @@ example( 13, gui => {

} );

example( 17, gui => {
example( 18, gui => {

const params = { feedback: 0 };

Expand All @@ -154,7 +154,7 @@ example( 17, gui => {

} );

example( 18, gui => {
example( 19, gui => {

let saved = {};

Expand Down
21 changes: 20 additions & 1 deletion src/utils/getColorFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ const ARRAY = {
}
};

// When getHex/setHex methods are available, prefer them over modifying RGB
// components directly. In some software (e.g. three.js and Blender), hex
// triplets are sRGB by convention, like the color picker's input and display,
// while RGB components are Linear sRGB.
const CUSTOM = {
isPrimitive: false,
match: v => Object( v ) === v && v.getHex && v.setHex,
fromHexString( string, target ) {

target.setHex( INT.fromHexString( string ) );

},
toHexString( target ) {

return INT.toHexString( target.getHex() );

}
};

const OBJECT = {
isPrimitive: false,
match: v => Object( v ) === v,
Expand All @@ -68,7 +87,7 @@ const OBJECT = {
}
};

const FORMATS = [ STRING, INT, ARRAY, OBJECT ];
const FORMATS = [ STRING, INT, ARRAY, CUSTOM, OBJECT ];

export default function( value ) {
return FORMATS.find( format => format.match( value ) );
Expand Down
28 changes: 28 additions & 0 deletions tests/color-parsing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import assert from 'assert';
import GUI from '../dist/lil-gui.esm.min.js';

import CallTracker from './utils/CallTracker.js';

export default () => {

const gui = new GUI();
Expand All @@ -21,6 +23,32 @@ export default () => {
assert.strictEqual( gui.addColor( { int }, 'int' ).$input.value, string );
assert.strictEqual( gui.addColor( { string }, 'string' ).$input.value, string );

// custom getHex/setHex methods

const getHexTracker = new CallTracker();
const setHexTracker = new CallTracker();

class CustomColorFormat {
_hex = 0x7a26ab;
getHex() {
getHexTracker.handler();
return this._hex;
}
setHex( hex ) {
setHexTracker.handler();
this._hex = hex;
}
}

const custom = new CustomColorFormat();
const customColorController = gui.addColor( { custom }, 'custom' );

assert.strictEqual( customColorController.$input.value, string );
assert( getHexTracker.numCalls > 0 );

customColorController._setValueFromHexString( '#334455' );
assert.strictEqual( setHexTracker.numCalls, 1 );

// todo: it doesn't get hit with any edge cases or malformed colors

};