Skip to content

Commit

Permalink
Allow Color.toColor to accept IColor type as argument, see: #1135
Browse files Browse the repository at this point in the history
  • Loading branch information
marlitas committed Jun 21, 2022
1 parent 800fa3e commit 2708621
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions js/util/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Utils from '../../../dot/js/Utils.js';
import IOType from '../../../tandem/js/types/IOType.js';
import NumberIO from '../../../tandem/js/types/NumberIO.js';
import { IPaint, scenery } from '../imports.js';
import IColor from './IColor.js';

// constants
const clamp = Utils.clamp;
Expand Down Expand Up @@ -562,19 +563,22 @@ export default class Color {
/**
* Convenience function that converts a color spec to a color object if necessary, or simply returns the color object
* if not.
*
* Please note there is no defensive copy when a color is passed in unlike PaintDef.
*/
static toColor( colorSpec: string | Color | null ): Color {
assert && assert( colorSpec === null || typeof colorSpec === 'string' || colorSpec instanceof Color );

static toColor( colorSpec: IColor ): Color {
if ( colorSpec === null ) {
return Color.TRANSPARENT;
}
else if ( colorSpec instanceof Color ) {
return colorSpec;
}
else {
else if ( typeof colorSpec === 'string' ) {
return new Color( colorSpec );
}
else {
return Color.toColor( colorSpec.value );
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions js/util/PaintDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const PaintDef = {

/**
* Takes a snapshot of the given paint, returning the current color where possible.
* Unlike Color.toColor() this method makes a defensive copy for Color values.
* @public
*
* @param {PaintDef} paint
Expand Down

0 comments on commit 2708621

Please sign in to comment.