From 2708621f15e1bf9204cf1c6888e54ffe2cd9544b Mon Sep 17 00:00:00 2001 From: Marla Schulz Date: Tue, 21 Jun 2022 15:54:33 -0600 Subject: [PATCH] Allow Color.toColor to accept IColor type as argument, see: https://github.com/phetsims/scenery/issues/1135 --- js/util/Color.ts | 12 ++++++++---- js/util/PaintDef.js | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/js/util/Color.ts b/js/util/Color.ts index 7ba60fa56..34b8c3a46 100644 --- a/js/util/Color.ts +++ b/js/util/Color.ts @@ -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; @@ -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 ); + } } /** diff --git a/js/util/PaintDef.js b/js/util/PaintDef.js index 3e76dea94..fc4640e64 100644 --- a/js/util/PaintDef.js +++ b/js/util/PaintDef.js @@ -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