import { assert } from "@here/harp-utils" ; import * as THREE from "three" ; import { RGBA } from "./RGBA" ; const SHIFT_TRANSPARENCY = 24 ; const SHIFT_RED = 16 ; const SHIFT_GREEN = 8 ; const SHIFT_BLUE = 0 ; const HEX_FULL_CHANNEL = 0xff ; const HEX_RGB_MASK = 0xffffff ; const HEX_TRGB_MASK = 0xffffffff ; const tmpColor = new THREE . Color ( ) ; /**
 * Utilities to convert RGBA colors encoded in custom number (hex) format to THREE.Color objects.
 *
 * The functions provided allows for conversion from and to our custom number based color format,
 * which contains transparency, red, green and blue color channels in a way that each channel
 * occupies 8 bits of resulting number (color format 0xTTRRGGBB).
 * In order to preserve compatibility with THREE.Color class and its hexadecimal color
 * representation, we do not store __alpha__ channel in encoded color's number, but replace it
 * with __transparency__ channel, which is simply opposite to alpha:
 * ```transparency = 0xFF - alpha```
 * Such channel value is stored on the oldest bits (octet) in the integral color (numeric) value,
 * so it is fully compatible with THREE.Color numerical representation (@see [[THREE.Color.getHex]],
 * [[THREE.Color.setHex]]).
 * See also [[getHexFromRgba]] and [[getRgbaFromHex]] for more info about conversion.
 */ export namespace ColorUtils { note note export function getHexFromRgba ( r , g , b , a ) { assert ( a >= 0 && a <= 1 ) ; const t = HEX_FULL_CHANNEL - Math . floor ( a * HEX_FULL_CHANNEL ) ; return ( ( t << SHIFT_TRANSPARENCY ) ^ ( ( r * HEX_FULL_CHANNEL ) << SHIFT_RED ) ^ ( ( g * HEX_FULL_CHANNEL ) << SHIFT_GREEN ) ^ ( ( b * HEX_FULL_CHANNEL ) << SHIFT_BLUE ) ) ; } note export function getHexFromRgb ( r , g , b ) { assert ( r >= 0 && r <= 1 ) ; assert ( g >= 0 && g <= 1 ) ; assert ( b >= 0 && b <= 1 ) ; return ( ( ( r * HEX_FULL_CHANNEL ) << SHIFT_RED ) ^ ( ( g * HEX_FULL_CHANNEL ) << SHIFT_GREEN ) ^ ( ( b * HEX_FULL_CHANNEL ) << SHIFT_BLUE ) ) ; } see getHexFromRgb . param h param s param l export function getHexFromHsl ( h , s , l ) { assert ( h >= 0 && h <= 1 ) ; assert ( s >= 0 && s <= 1 ) ; assert ( l >= 0 && l <= 1 ) ; return tmpColor . setHSL ( h , s , l ) . getHex ( ) ; } see getHexFromRgba . param hex returns export function getRgbaFromHex ( hex , target = new RGBA ( ) ) { assert ( ( hex & ~ HEX_TRGB_MASK ) === 0 , "Wrong hex format" ) ; target . r = ( ( hex >> SHIFT_RED ) & HEX_FULL_CHANNEL ) / HEX_FULL_CHANNEL ; target . g = ( ( hex >> SHIFT_GREEN ) & HEX_FULL_CHANNEL ) / HEX_FULL_CHANNEL ; target . b = ( ( hex >> SHIFT_BLUE ) & HEX_FULL_CHANNEL ) / HEX_FULL_CHANNEL ; target . a = ( HEX_FULL_CHANNEL - ( ( hex >> SHIFT_TRANSPARENCY ) & HEX_FULL_CHANNEL ) ) / HEX_FULL_CHANNEL ; return target ; } param hex returns export function hasAlphaInHex ( hex ) { assert ( ( hex & ~ HEX_TRGB_MASK ) === 0 , "Wrong hex format" ) ; return hex >> SHIFT_TRANSPARENCY !== 0 ; } see getHexFromRgba . param hex returns export function getAlphaFromHex ( hex ) { assert ( ( hex & ~ HEX_TRGB_MASK ) === 0 , "Wrong hex format" ) ; return ( ( ( HEX_FULL_CHANNEL - ( hex >> SHIFT_TRANSPARENCY ) ) & HEX_FULL_CHANNEL ) / HEX_FULL_CHANNEL ) ; } see getAlphaFromHex . param hex returns export function removeAlphaFromHex ( hex ) { assert ( ( hex & ~ HEX_TRGB_MASK ) === 0 , "Wrong hex format" ) ; return hex & HEX_RGB_MASK ; } }