import { Texture } from '@pixi/core' ; import { IPointData } from '@pixi/math' ; import { BLEND_MODES } from '@pixi/constants' ; import { PropertyNode , ValueStep } from './PropertyNode' ; /**
 * The method used by behaviors to fetch textures. Defaults to Texture.from.
 */ export let GetTextureFromString = Texture . from ; /**
 * A color value, split apart for interpolation.
 */ export interface Color { r : number ; g : number ; b : number ; a ? : number ; } export interface EaseSegment { cp : number ; s : number ; e : number ; } /**
 * The basic easing function used. Takes in a value between 0-1, and outputs another value between 0-1.
 * For example, a basic quadratic in ease would be `(time) => time * time`
 */ export type SimpleEase = ( time : number ) => number ; /**
 * If errors and warnings should be logged within the library.
 */ export const verbose = false ; export const DEG_TO_RADS = Math . PI / 180 ; param angle param p export function rotatePoint ( angle , p ) { if ( ! angle ) return ; const s = Math . sin ( angle ) ; const c = Math . cos ( angle ) ; const xnew = ( p . x * c ) - ( p . y * s ) ; const ynew = ( p . x * s ) + ( p . y * c ) ; p . x = xnew ; p . y = ynew ; } param r param g param b return export function combineRGBComponents ( r , g , b ) { return ( r << 16 ) | ( g << 8 ) | b ; } param point return export function length ( point ) { return Math . sqrt ( ( point . x * point . x ) + ( point . y * point . y ) ) ; } param point export function normalize ( point ) { const oneOverLen = 1 / length ( point ) ; point . x *= oneOverLen ; point . y *= oneOverLen ; } param point param value export function scaleBy ( point , value ) { point . x *= value ; point . y *= value ; } param color param output return export function hexToRGB ( color , output ? ) { if ( ! output ) { output = { } as Color ; } if ( color . charAt ( 0 ) === '#' ) { color = color . substr ( 1 ) ; } else if ( color . indexOf ( '0x' ) === 0 ) { color = color . substr ( 2 ) ; } let alpha ; if ( color . length === 8 ) { alpha = color . substr ( 0 , 2 ) ; color = color . substr ( 2 ) ; } output . r = parseInt ( color . substr ( 0 , 2 ) , 16 ) ; output . g = parseInt ( color . substr ( 2 , 2 ) , 16 ) ; output . b = parseInt ( color . substr ( 4 , 2 ) , 16 ) ; if ( alpha ) { output . a = parseInt ( alpha , 16 ) ; } return output ; } param segments return export function generateEase ( segments ) { const qty = segments . length ; const oneOverQty = 1 / qty ; return function ( time ) : number { const i = ( qty * time ) | 0 ; const t = ( time - ( i * oneOverQty ) ) * qty ; const s = segments [ i ] || segments [ qty - 1 ] ; return ( s . s + ( t * ( ( 2 * ( 1 - t ) * ( s . cp - s . s ) ) + ( t * ( s . e - s . s ) ) ) ) ) ; } ; } param name return export function getBlendMode ( name ) { if ( ! name ) return BLEND_MODES . NORMAL ; name = name . toUpperCase ( ) . replace ( / /g , '_' ) ; return ( BLEND_MODES as any ) [ name ] || BLEND_MODES . NORMAL ; } param list param numSteps return export function createSteppedGradient ( list , numSteps = 10 ) { if ( typeof numSteps !== 'number' || numSteps <= 0 ) { numSteps = 10 ; } const first = new PropertyNode < Color > ( hexToRGB ( list [ 0 ] . value ) , list [ 0 ] . time ) ; first . isStepped = true ; let currentNode = first ; let current = list [ 0 ] ; let nextIndex = 1 ; let next = list [ nextIndex ] ; for ( let i = 1 ; i < numSteps ; ++ i ) { let lerp = i / numSteps ; while ( lerp > next . time ) { current = next ; next = list [ ++ nextIndex ] ; } lerp = ( lerp - current . time ) / ( next . time - current . time ) ; const curVal = hexToRGB ( current . value ) ; const nextVal = hexToRGB ( next . value ) ; const output = { r : ( ( nextVal . r - curVal . r ) * lerp ) + curVal . r , g : ( ( nextVal . g - curVal . g ) * lerp ) + curVal . g , b : ( ( nextVal . b - curVal . b ) * lerp ) + curVal . b , } ; currentNode . next = new PropertyNode ( output , i / numSteps ) ; currentNode = currentNode . next ; } return first ; }