import Color , { IColor } from '../colors/color' ; import '../colors/strategies/hexa-strategy' ; import '../colors/strategies/argb-strategy' ; import '../colors/strategies/rgb-strategy' ; import '../colors/strategies/browser-strategy' ; import '../colors/strategies/hsl-strategy' ; import ColorExtractor from '../colors/color-extractor' ; import ColorDecoration from '../colors/color-decoration' ; import { Range , TextEditorDecorationType } from 'vscode' ; interface DocumentLine { line : number ; text : string ; } interface LineExtraction { line : number ; colors : IColor [ ] ; } const flattenLineExtractionsFlatten = arr => arr . flat ( 2 ) . filter ( _ => _ . colors . length !== 0 ) ; const WHITE = '#FFFFFF' , BLACK = '#000000' ; interface IDecoration { decoration : TextEditorDecorationType ; rgb : number [ ] ; currentRange : Range ; public memberOf dispose ( ) : void ; public memberOf hide ( ) : void ; public memberOf disposed : boolean ; param { number } line returns { Range } memberOf generateRange ( line ) : Range ; returns { boolean } memberof shouldGenerateDecoration ( ) : boolean ; } class ColorUtil { public static textToFileLines ( text ) { return text . split ( /\n/ ) . map ( ( text , index ) => Object ( { 'text' : text , 'line' : index } ) ) ; } static param { any } text returns { Promise < Color [ ] > } memberOf public static findColors ( fileContent ) { return ColorExtractor . extract ( fileContent ) ; } public static setupColorsExtractors ( extractors ) { ColorExtractor . enableStrategies ( extractors ) ; } public static generateDecoration ( color , line , decorationFn ) { return new ColorDecoration ( < Color > color , line , decorationFn ) ; } } static param { Color } color returns { number } function colorLuminance ( color ) { const rgb = color . rgb . map ( c => { c = c / 255 ; if ( c < 0.03928 ) { c = c / 12.92 ; } else { c = ( c + .055 ) / 1.055 ; c = Math . pow ( c , 2.4 ) ; } return c ; } ) ; return ( 0.2126 * rgb [ 0 ] + 0.7152 * rgb [ 1 ] + 0.0722 * rgb [ 2 ] ) ; } function generateOptimalTextColor ( color ) { const luminance = colorLuminance ( color ) ; const contrastRatioBlack = ( luminance + 0.05 ) / 0.05 ; if ( contrastRatioBlack > 7 ) { return BLACK ; } const contrastRatioWhite = 1.05 / ( luminance + 0.05 ) ; if ( contrastRatioWhite > 7 ) { return WHITE ; } if ( contrastRatioBlack > contrastRatioWhite ) { return BLACK ; } return WHITE ; } param { number } r param { number } g param { number } b param { number } a return { [ number , number , number , number ] } function convertRgbaToHsla ( r , g , b , a = 1 ) { r /= 255 , g /= 255 , b /= 255 ; const max = Math . max ( r , g , b ) , min = Math . min ( r , g , b ) ; let h ; const l = ( max + min ) / 2 ; if ( max === min ) { return [ 0 , 0 , Math . round ( l * 100 ) , a ] ; } const d = max - min ; const s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min ) ; switch ( max ) { case r : h = ( g - b ) / d + ( g < b ? 6 : 0 ) ; break ; case g : h = ( b - r ) / d + 2 ; break ; case b : h = ( r - g ) / d + 4 ; break ; } h /= 6 ; return [ Math . round ( 360 * h ) , Math . round ( 100 * s ) , Math . round ( l * 100 ) , a ] ; } param { number } h param { number } s param { number } l param { number } a return { [ number , number , number , number ] } function convertHslaToRgba ( h , s , l , a = 1 ) { let r , g , b ; if ( s === 0 ) { r = g = b = Math . round ( ( l / 100 ) * 255 ) ; return [ r , g , b , a ] ; } l = l / 100 ; s = s / 100 ; const tmp_1 = ( l < 0.5 ) ? l * ( 1.0 + s ) : l + s - l * s ; const temp_2 = 2 * l - tmp_1 ; h = ( h % 360 ) / 360 ; const tmp_r = ( h + 0.333 ) % 1 ; const tmp_g = h ; let tmp_b = h - 0.333 ; if ( tmp_b < 0 ) { tmp_b = tmp_b + 1 ; } r = executeHSLProperFormula ( tmp_1 , temp_2 , tmp_r ) ; g = executeHSLProperFormula ( tmp_1 , temp_2 , tmp_g ) ; b = executeHSLProperFormula ( tmp_1 , temp_2 , tmp_b ) ; return [ Math . round ( r * 255 ) , Math . round ( g * 255 ) , Math . round ( b * 255 ) , a ] ; } private param { number } tmp_1 param { number } tmp_2 param { number } value returns { number } memberof function executeHSLProperFormula ( tmp_1 , tmp_2 , value ) { const res = tmp_2 ; if ( 6 * value < 1 ) { return tmp_2 + ( tmp_1 - tmp_2 ) * 6 * value ; } if ( 2 * value < 1 ) { return tmp_1 ; } if ( 3 * value < 2 ) { return tmp_2 + ( tmp_1 - tmp_2 ) * ( ( 2 / 3 ) - value ) * 6 ; } return res ; } export default ColorUtil ; export { IDecoration , convertHslaToRgba , colorLuminance , convertRgbaToHsla , generateOptimalTextColor , flattenLineExtractionsFlatten , LineExtraction , DocumentLine } ;