Skip to content
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
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 123 additions & 34 deletions src/math/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,84 +285,173 @@ class Color {
*/
setStyle( style, colorSpace = SRGBColorSpace ) {

function handleAlpha( string ) {
function handleAlpha( value ) {

if ( string === undefined ) return;
if ( value === undefined ) return;

if ( parseFloat( string ) < 1 ) {
if ( value < 1 ) {

warn( 'Color: Alpha component of ' + style + ' will be ignored.' );

}

}

function parseAlpha( string ) {

if ( string === undefined ) return undefined;

const match = /^([+-]?\d*\.?\d+)(%)?$/.exec( string.trim() );
if ( match === null ) return null;

let alpha = parseFloat( match[ 1 ] );

if ( match[ 2 ] === '%' ) {

alpha /= 100;

}

return clamp( alpha, 0, 1 );

}

function parseFunctionComponents( components ) {

if ( components.includes( ',' ) ) {

const parts = components.split( /\s*,\s*/ );
if ( parts.length !== 3 && parts.length !== 4 ) return null;

if ( parts.some( ( part ) => part.length === 0 ) ) return null;

return {
values: parts.slice( 0, 3 ),
alpha: parts[ 3 ]
};

}

const slashParts = components.split( /\s*\/\s*/ );

if ( slashParts.length > 2 ) return null;

const values = slashParts[ 0 ].trim().split( /\s+/ );
if ( values.length !== 3 || values.some( ( value ) => value.length === 0 ) ) return null;

return {
values: values,
alpha: slashParts[ 1 ]
};

}

function parseRGBValue( string ) {

const match = /^([+-]?\d*\.?\d+)(%)?$/.exec( string.trim() );
if ( match === null ) return null;

let value = parseFloat( match[ 1 ] );

if ( match[ 2 ] === '%' ) {

value = Math.min( 100, value ) / 100;

} else {

value = Math.min( 255, value ) / 255;

}

return value;

}

function parseHue( string ) {

const match = /^([+-]?\d*\.?\d+)(deg)?$/i.exec( string.trim() );
if ( match === null ) return null;

return parseFloat( match[ 1 ] );

}

function parsePercent( string ) {

const match = /^([+-]?\d*\.?\d+)%$/.exec( string.trim() );
if ( match === null ) return null;

return parseFloat( match[ 1 ] ) / 100;

}


let m;

if ( m = /^(\w+)\(([^\)]*)\)/.exec( style ) ) {
if ( m = /^\s*([A-Za-z]+)\(([^\)]*)\)\s*$/.exec( style ) ) {

// rgb / hsl

let color;
const name = m[ 1 ];
const name = m[ 1 ].toLowerCase();
const components = m[ 2 ];

switch ( name ) {

case 'rgb':
case 'rgba':

if ( color = /^\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {

// rgb(255,0,0) rgba(255,0,0,0.5)
color = parseFunctionComponents( components );

handleAlpha( color[ 4 ] );
if ( color !== null ) {

return this.setRGB(
Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255,
Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255,
Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255,
colorSpace
);
const alpha = parseAlpha( color.alpha );
const r = parseRGBValue( color.values[ 0 ] );
const g = parseRGBValue( color.values[ 1 ] );
const b = parseRGBValue( color.values[ 2 ] );

}
if ( alpha !== null && r !== null && g !== null && b !== null ) {

if ( color = /^\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
// rgb(255,0,0), rgb(255 0 0), rgb(255 0 0 / 50%), rgba(...)

// rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
handleAlpha( alpha );

handleAlpha( color[ 4 ] );
return this.setRGB( r, g, b, colorSpace );

return this.setRGB(
Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100,
Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100,
Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100,
colorSpace
);
}

}

warn( 'Color: Invalid color components ' + style );

break;

case 'hsl':
case 'hsla':

if ( color = /^\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\%\s*,\s*(\d*\.?\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
color = parseFunctionComponents( components );

if ( color !== null ) {

const alpha = parseAlpha( color.alpha );
const h = parseHue( color.values[ 0 ] );
const s = parsePercent( color.values[ 1 ] );
const l = parsePercent( color.values[ 2 ] );

// hsl(120,50%,50%) hsla(120,50%,50%,0.5)
if ( alpha !== null && h !== null && s !== null && l !== null ) {

handleAlpha( color[ 4 ] );
// hsl(120,50%,50%), hsl(120deg 50% 50% / 50%), hsla(...)

return this.setHSL(
parseFloat( color[ 1 ] ) / 360,
parseFloat( color[ 2 ] ) / 100,
parseFloat( color[ 3 ] ) / 100,
colorSpace
);
handleAlpha( alpha );

return this.setHSL( h / 360, s, l, colorSpace );

}

}

warn( 'Color: Invalid color components ' + style );

break;

default:
Expand Down
94 changes: 94 additions & 0 deletions test/unit/src/math/Color.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,50 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( 'setStyleRGBSpaceSeparated', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();
c.setStyle( 'rgb(255 0 0)' );
assert.ok( c.r == 1, 'Red: ' + c.r );
assert.ok( c.g === 0, 'Green: ' + c.g );
assert.ok( c.b === 0, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleRGBSpaceSeparatedWithAlpha', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();

console.level = CONSOLE_LEVEL.ERROR;
c.setStyle( 'rgb(255 0 0 / 0.5)' );
console.level = CONSOLE_LEVEL.DEFAULT;

assert.ok( c.r == 1, 'Red: ' + c.r );
assert.ok( c.g === 0, 'Green: ' + c.g );
assert.ok( c.b === 0, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleRGBPercentAlphaPercent', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();

console.level = CONSOLE_LEVEL.ERROR;
c.setStyle( 'rgb(100%,50%,10%,50%)' );
console.level = CONSOLE_LEVEL.DEFAULT;

assert.ok( c.r == 1, 'Red: ' + c.r );
assert.ok( c.g == 0.5, 'Green: ' + c.g );
assert.ok( c.b == 0.1, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleHSLRed', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.
Expand Down Expand Up @@ -774,6 +818,56 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( 'setStyleHSLSpaceSeparatedWithDegrees', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();
c.setStyle( 'hsl(120deg 100% 50%)' );
assert.ok( Math.abs( c.r ) <= eps, 'Red: ' + c.r );
assert.ok( c.g == 1, 'Green: ' + c.g );
assert.ok( Math.abs( c.b ) <= eps, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleUpperCaseRGBModel', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();
c.setStyle( 'RGB(255,0,0)' );
assert.ok( c.r == 1, 'Red: ' + c.r );
assert.ok( c.g === 0, 'Green: ' + c.g );
assert.ok( c.b === 0, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleUpperCaseHSLModel', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color();
c.setStyle( 'HSL(120,100%,50%)' );
assert.ok( Math.abs( c.r ) <= eps, 'Red: ' + c.r );
assert.ok( c.g == 1, 'Green: ' + c.g );
assert.ok( Math.abs( c.b ) <= eps, 'Blue: ' + c.b );

} );

QUnit.test( 'setStyleInvalidKnownModelDoesNotApply', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.

const c = new Color( 0x123456 );

console.level = CONSOLE_LEVEL.ERROR;
c.setStyle( 'rgb(255,0,oops)' );
console.level = CONSOLE_LEVEL.DEFAULT;

assert.strictEqual( c.getHex(), 0x123456, 'Invalid known-model components should not mutate color.' );

} );

QUnit.test( 'setStyleHexSkyBlue', ( assert ) => {

ColorManagement.enabled = false; // TODO: Update and enable.
Expand Down