import { HsvColor } from '@uiw/color-convert' ; import { WheelProps } from './' ; const TAU = Math . PI * 2 ; /**
 * javascript's modulo operator doesn't produce positive numbers with negative input
 * https://www.geeksforgeeks.org/how-to-get-negative-result-using-modulo-operator-in-javascript/
 */ export const mod = ( a , n ) => ( ( a % n ) + n ) % n ; /** distance between points (x, y) and (0, 0) */ const dist = ( x , y ) => Math . sqrt ( x * x + y * y ) ; /**
 * Get the point as the center of the wheel
 */ export function getWheelDimensions ( { width = 0 } ) { const r = width / 2 ; return { width , radius : r , cx : r , cy : r , } ; } /**
 * Returns true if point (x, y) lands inside the wheel
 */ export function isInputInsideWheel ( props , x , y ) { const { cx , cy , width } = getWheelDimensions ( props ) ; const r = width / 2 ; return dist ( cx - x , cy - y ) < r ; } /**
 * Get the current handle position for a given color
 */ export function getWheelHandlePosition ( props , hsv ) { const { cx , cy } = getWheelDimensions ( props ) ; const handleRange = getHandleRange ( props ) ; const handleAngle = ( 180 + translateWheelAngle ( props , hsv . h , true ) ) * ( TAU / 360 ) ; const handleDist = ( hsv . s / 100 ) * handleRange ; const direction = props . direction === 'clockwise' ? - 1 : 1 ; return { x : cx + handleDist * Math . cos ( handleAngle ) * direction , y : cy + handleDist * Math . sin ( handleAngle ) * direction , } ; } /**
 * Get Range
 */ export function getHandleRange ( { width = 0 } ) { return width / 2 ; } /**
 * Translate an angle according to wheelAngle and wheelDirection
 */ export function translateWheelAngle ( props , angle , invert ? ) { const wheelAngle = props . angle || 0 ; const direction = props . direction ; if ( invert && direction === 'clockwise' ) angle = wheelAngle + angle ; else if ( direction === 'clockwise' ) angle = 360 - wheelAngle + angle ; else if ( invert && direction === 'anticlockwise' ) angle = wheelAngle + 180 - angle ; else if ( direction === 'anticlockwise' ) angle = wheelAngle - angle ; return mod ( angle , 360 ) ; } param props param x param y export function getWheelValueFromInput ( props , x , y ) { const { cx , cy } = getWheelDimensions ( props ) ; const handleRange = getHandleRange ( props ) ; x = cx - x ; y = cy - y ; const hue = translateWheelAngle ( props , Math . atan2 ( - y , - x ) * ( 360 / TAU ) ) ; const handleDist = Math . min ( dist ( x , y ) , handleRange ) ; return { h : Math . round ( hue ) , s : Math . round ( ( 100 / handleRange ) * handleDist ) , } ; }