import { utils } from "pixi.js" ; /**
 * 颜色工具
 */ export class ColorUtils { param hex public static hex2string ( hex ) { return utils . hex2string ( hex ) ; } param hex public static hex2rgbRadix ( hex ) { return utils . hex2rgb ( hex ) ; } param hex public static hex2rgb ( hex ) { return this . rgbRadix2rgb ( this . hex2rgbRadix ( hex ) ) ; } param hex public static string2hex ( str ) { return utils . string2hex ( str ) ; } param str public static string2rgb ( str ) { return this . hex2rgb ( this . string2hex ( str ) ) ; } param str public static string2rgbRadix ( str ) { return utils . hex2rgb ( this . string2hex ( str ) ) ; } param rgb public static rgbRadix2hex ( rgb ) { if ( rgb . length != 3 || isNaN ( rgb [ 0 ] ) || isNaN ( rgb [ 1 ] ) || isNaN ( rgb [ 2 ] ) ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[1, 1, 1]` ) ; } return utils . rgb2hex ( rgb ) ; } param rgb public static rgbRadix2string ( rgb ) { return this . hex2string ( this . rgbRadix2hex ( rgb ) ) ; } param rgb public static rgbRadix2rgb ( rgb ) { if ( rgb . length != 3 || isNaN ( rgb [ 0 ] ) || isNaN ( rgb [ 1 ] ) || isNaN ( rgb [ 2 ] ) ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[1, 1, 1]` ) ; } if ( rgb [ 0 ] <= 1 && rgb [ 1 ] <= 1 && rgb [ 2 ] <= 1 ) { rgb [ 0 ] *= 255 ; rgb [ 1 ] *= 255 ; rgb [ 2 ] *= 255 ; } if ( rgb [ 0 ] > 255 || rgb [ 1 ] > 255 || rgb [ 2 ] > 255 ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[1, 1, 1]` ) ; } return rgb ; } param rgb public static rgb2hex ( rgb ) { if ( rgb . length != 3 || isNaN ( rgb [ 0 ] ) || isNaN ( rgb [ 1 ] ) || isNaN ( rgb [ 2 ] ) ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[255, 255, 255]` ) ; } return this . rgbRadix2hex ( this . rgb2rgbRadix ( rgb ) ) ; } param rgb public static rgb2string ( rgb ) { if ( rgb . length != 3 || isNaN ( rgb [ 0 ] ) || isNaN ( rgb [ 1 ] ) || isNaN ( rgb [ 2 ] ) ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[255, 255, 255]` ) ; } return this . rgbRadix2string ( this . rgb2rgbRadix ( rgb ) ) ; } param rgb public static rgb2rgbRadix ( rgb ) { if ( rgb . length != 3 || isNaN ( rgb [ 0 ] ) || isNaN ( rgb [ 1 ] ) || isNaN ( rgb [ 2 ] ) ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[255, 255, 255]` ) ; } if ( rgb [ 0 ] <= 255 && rgb [ 1 ] <= 255 && rgb [ 2 ] <= 255 ) { rgb [ 0 ] /= 255 ; rgb [ 1 ] /= 255 ; rgb [ 2 ] /= 255 ; } if ( rgb [ 0 ] > 1 || rgb [ 1 ] > 1 || rgb [ 2 ] > 1 ) { throw new Error ( `rgb 颜色出错${ rgb . toString ( ) },应该为[255, 255, 255]` ) ; } return rgb ; } }