import { Math2D , MathUtils } from "@here/harp-utils" ; param xPos param yPos param image param canvas export function getPixelFromImage ( xPos , yPos , image , canvas ? ) { if ( image instanceof ImageData ) { const stride = image . data . length / ( image . height * image . width ) ; return getPixelFromImageData ( image , xPos , yPos , stride ) ; } if ( ! canvas ) { canvas = document . createElement ( "canvas" ) ; } return getPixelFromCanvasImageSource ( image , xPos , yPos , canvas ) ; } param screenX param screenY param box param uvBox export function screenToUvCoordinates ( screenX , screenY , box , uvBox ) { const minX = box . x ; const maxX = box . x + box . w ; const minY = box . y ; const maxY = box . y + box . h ; const u = MathUtils . map ( screenX , minX , maxX , uvBox . s0 , uvBox . s1 ) ; const v = MathUtils . map ( screenY , minY , maxY , uvBox . t0 , uvBox . t1 ) ; return { u , v } ; } param image param xPos param yPos param canvas export function getPixelFromCanvasImageSource ( image , xPos , yPos , canvas ) { const { width , height } = image instanceof SVGImageElement ? image . getBBox ( ) : image ; if ( xPos > width || xPos < 0 || yPos > height || yPos < 0 ) { return undefined ; } let pixelData ; canvas . width = width ; canvas . height = height ; const context = canvas . getContext ( "2d" ) ; if ( context !== null ) { context . drawImage ( image , 0 , 0 ) ; pixelData = context . getImageData ( xPos , yPos , 1 , 1 ) . data ; } return pixelData ; } param image param xPos param yPos param stride export function getPixelFromImageData ( imgData , xPos , yPos , stride ) { const getPixel = ( imageData , index , strd ) => { const i = index * strd ; const d = imageData . data ; const pixel = new Uint8ClampedArray ( strd ) ; for ( let s = 0 ; s < strd ; s ++ ) { pixel [ 0 ] = d [ i + s ] ; } return pixel ; } ; if ( xPos > imgData . width || xPos < 0 || yPos > imgData . height || yPos < 0 ) { return undefined ; } return getPixel ( imgData , yPos * imgData . width + xPos , stride ) ; }