Skip to content

Commit

Permalink
Converting dot Utils usage, see phetsims/dot#4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Feb 14, 2025
1 parent 5d4335f commit 3b0c559
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions js/filters/HueRotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import toSVGNumber from '../../../dot/js/toSVGNumber.js';
import Utils from '../../../dot/js/Utils.js';
import ColorMatrixFilter from '../filters/ColorMatrixFilter.js';
import scenery from '../scenery.js';
import { toDegrees } from '../../../dot/js/util/toDegrees.js';

export default class HueRotate extends ColorMatrixFilter {

Expand Down Expand Up @@ -51,7 +51,7 @@ export default class HueRotate extends ColorMatrixFilter {
* (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter).
*/
public override getCSSFilterString(): string {
return `hue-rotate(${toSVGNumber( Utils.toDegrees( this.amount ) )}deg)`;
return `hue-rotate(${toSVGNumber( toDegrees( this.amount ) )}deg)`;
}

public override isDOMCompatible(): boolean {
Expand Down
4 changes: 2 additions & 2 deletions js/layout/constraints/MarginLayoutCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Bounds2 from '../../../../dot/js/Bounds2.js';
import Matrix3 from '../../../../dot/js/Matrix3.js';
import Utils from '../../../../dot/js/Utils.js';
import Shape from '../../../../kite/js/Shape.js';
import Orientation from '../../../../phet-core/js/Orientation.js';
import OrientationPair from '../../../../phet-core/js/OrientationPair.js';
Expand All @@ -27,6 +26,7 @@ import scenery from '../../scenery.js';
import Font from '../../util/Font.js';
import NodePattern from '../../util/NodePattern.js';
import type TColor from '../../util/TColor.js';
import { clamp } from '../../../../dot/js/util/clamp.js';

// Interface expected to be overridden by subtypes (GridCell, FlowCell)
export type MarginLayout = {
Expand Down Expand Up @@ -244,7 +244,7 @@ export default class MarginLayoutCell extends LayoutCell {
assert && assert( isFinite( minimumSize ) );
assert && assert( maximumSize >= minimumSize );

value = Utils.clamp( value, minimumSize, maximumSize );
value = clamp( value, minimumSize, maximumSize );

let preferredSize = value - this.getEffectiveMinMargin( orientation ) - this.getEffectiveMaxMargin( orientation );
const maxSize = this.proxy.getMax( orientation );
Expand Down
7 changes: 4 additions & 3 deletions js/listeners/AnimatedPanZoomListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import BooleanProperty from '../../../axon/js/BooleanProperty.js';
import { PropertyLinkListener } from '../../../axon/js/TReadOnlyProperty.js';
import Bounds2 from '../../../dot/js/Bounds2.js';
import Matrix3 from '../../../dot/js/Matrix3.js';
import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import optionize from '../../../phet-core/js/optionize.js';
import platform from '../../../phet-core/js/platform.js';
Expand Down Expand Up @@ -46,6 +45,8 @@ import { pdomFocusProperty } from '../accessibility/pdomFocusProperty.js';
import IntentionalAny from '../../../phet-core/js/types/IntentionalAny.js';
import { TReadOnlyEmitter } from '../../../axon/js/TEmitter.js';
import stepTimer from '../../../axon/js/stepTimer.js';
import { equalsEpsilon } from '../../../dot/js/util/equalsEpsilon.js';
import { clamp } from '../../../dot/js/util/clamp.js';

// constants
const MOVE_CURSOR = 'all-scroll';
Expand Down Expand Up @@ -1096,7 +1097,7 @@ class AnimatedPanZoomListener extends PanZoomListener {
// only animate to targets if within this precision so that we don't animate forever, since animation speed
// is dependent on the difference betwen source and destination positions
const positionDirty = !destinationPosition.equalsEpsilon( sourcePosition, 0.1 );
const scaleDirty = !Utils.equalsEpsilon( this.sourceScale, this.destinationScale, 0.001 );
const scaleDirty = !equalsEpsilon( this.sourceScale, this.destinationScale, 0.001 );

this.animatingProperty.value = positionDirty || scaleDirty;

Expand Down Expand Up @@ -1295,7 +1296,7 @@ class AnimatedPanZoomListener extends PanZoomListener {
nearestIndex = nearestIndex!;
assert && assert( nearestIndex !== null, 'nearestIndex should have been found' );
let nextIndex = zoomIn ? nearestIndex + 1 : nearestIndex - 1;
nextIndex = Utils.clamp( nextIndex, 0, this.discreteScales.length - 1 );
nextIndex = clamp( nextIndex, 0, this.discreteScales.length - 1 );
return this.discreteScales[ nextIndex ];
}

Expand Down
9 changes: 5 additions & 4 deletions js/nodes/Imageable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TinyForwardingProperty from '../../../axon/js/TinyForwardingProperty.js';
import TProperty from '../../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js';
import Matrix3 from '../../../dot/js/Matrix3.js';
import Utils from '../../../dot/js/Utils.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';
import Vector2 from '../../../dot/js/Vector2.js';
import Shape from '../../../kite/js/Shape.js';
import cleanArray from '../../../phet-core/js/cleanArray.js';
Expand All @@ -21,6 +21,7 @@ import IntentionalAny from '../../../phet-core/js/types/IntentionalAny.js';
import scenery from '../scenery.js';
import svgns from '../util/svgns.js';
import xlinkns from '../util/xlinkns.js';
import { clamp } from '../../../dot/js/util/clamp.js';

// Need to poly-fill on some browsers
const log2 = Math.log2 || function( x: number ) { return Math.log( x ) / Math.LN2; };
Expand Down Expand Up @@ -876,7 +877,7 @@ const Imageable = <SuperType extends Constructor>( type: SuperType ): SuperType
let level = log2( 1 / scale );

// convert to an integer level (-0.7 is a good default)
level = Utils.roundSymmetric( level + this._mipmapBias + additionalBias - 0.7 );
level = roundSymmetric( level + this._mipmapBias + additionalBias - 0.7 );

if ( level < 0 ) {
level = 0;
Expand Down Expand Up @@ -1091,8 +1092,8 @@ Imageable.getHitTestData = ( image: ParsedImage, width: number, height: number )
*/
Imageable.testHitTestData = ( imageData: ImageData, width: number, height: number, point: Vector2 ): boolean => {
// For sanity, map it based on the image dimensions and image data dimensions, and carefully clamp in case things are weird.
const x = Utils.clamp( Math.floor( ( point.x / width ) * imageData.width ), 0, imageData.width - 1 );
const y = Utils.clamp( Math.floor( ( point.y / height ) * imageData.height ), 0, imageData.height - 1 );
const x = clamp( Math.floor( ( point.x / width ) * imageData.width ), 0, imageData.width - 1 );
const y = clamp( Math.floor( ( point.y / height ) * imageData.height ), 0, imageData.height - 1 );

const index = 4 * ( x + y * imageData.width ) + 3;

Expand Down
22 changes: 10 additions & 12 deletions js/util/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
import type TEmitter from '../../../axon/js/TEmitter.js';
import TinyEmitter from '../../../axon/js/TinyEmitter.js';
import { isTReadOnlyProperty } from '../../../axon/js/TReadOnlyProperty.js';
import Utils from '../../../dot/js/Utils.js';
import { clamp } from '../../../dot/js/util/clamp.js';
import IOType from '../../../tandem/js/types/IOType.js';
import NumberIO from '../../../tandem/js/types/NumberIO.js';
import scenery from '../scenery.js';
import type TPaint from '../util/TPaint.js';
import type TColor from './TColor.js';

// constants
const clamp = Utils.clamp;
const linear = Utils.linear;
import { linear } from '../../../dot/js/util/linear.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';

type FormatParser = {
regexp: RegExp;
Expand All @@ -44,7 +42,7 @@ function parseRGBNumber( str: string ): number {
str = str.slice( 0, str.length - 1 );
}

return Utils.roundSymmetric( Number( str ) * multiplier );
return roundSymmetric( Number( str ) * multiplier );
}

export default class Color {
Expand Down Expand Up @@ -235,9 +233,9 @@ export default class Color {
* Sets the value of this Color using RGB integral between 0-255, alpha (float) between 0-1.
*/
public setRGBA( red: number, green: number, blue: number, alpha: number ): this {
this.r = Utils.roundSymmetric( clamp( red, 0, 255 ) );
this.g = Utils.roundSymmetric( clamp( green, 0, 255 ) );
this.b = Utils.roundSymmetric( clamp( blue, 0, 255 ) );
this.r = roundSymmetric( clamp( red, 0, 255 ) );
this.g = roundSymmetric( clamp( green, 0, 255 ) );
this.b = roundSymmetric( clamp( blue, 0, 255 ) );
this.a = clamp( alpha, 0, 1 );

this.updateColor(); // update the cached value
Expand Down Expand Up @@ -409,9 +407,9 @@ export default class Color {
}
const m1 = lightness * 2 - m2;

this.r = Utils.roundSymmetric( Color.hueToRGB( m1, m2, hue + 1 / 3 ) * 255 );
this.g = Utils.roundSymmetric( Color.hueToRGB( m1, m2, hue ) * 255 );
this.b = Utils.roundSymmetric( Color.hueToRGB( m1, m2, hue - 1 / 3 ) * 255 );
this.r = roundSymmetric( Color.hueToRGB( m1, m2, hue + 1 / 3 ) * 255 );
this.g = roundSymmetric( Color.hueToRGB( m1, m2, hue ) * 255 );
this.b = roundSymmetric( Color.hueToRGB( m1, m2, hue - 1 / 3 ) * 255 );
this.a = clamp( alpha, 0, 1 );

this.updateColor(); // update the cached value
Expand Down
4 changes: 2 additions & 2 deletions js/util/rasterizeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Node, { NodeOptions } from '../nodes/Node.js';
import deprecationWarning from '../../../phet-core/js/deprecationWarning.js';
import optionize, { combineOptions } from '../../../phet-core/js/optionize.js';
import Bounds2 from '../../../dot/js/Bounds2.js';
import Utils from '../../../dot/js/Utils.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';

export type RasterizedOptions = {

Expand Down Expand Up @@ -135,7 +135,7 @@ export function rasterizeNode( node: Node, providedOptions?: RasterizedOptions )
}

// NOTE: Rounding necessary due to floating point arithmetic in the width/height computation of the bounds
tempWrapperNode.toCanvas( callback, -transformedBounds.minX, -transformedBounds.minY, Utils.roundSymmetric( transformedBounds.width ), Utils.roundSymmetric( transformedBounds.height ) );
tempWrapperNode.toCanvas( callback, -transformedBounds.minX, -transformedBounds.minY, roundSymmetric( transformedBounds.width ), roundSymmetric( transformedBounds.height ) );

assert && assert( imageOrNull, 'The toCanvas should have executed synchronously' );
const image = imageOrNull!;
Expand Down

0 comments on commit 3b0c559

Please sign in to comment.