@@ -4,60 +4,56 @@ import type { OptionValue, paletteT } from './types'
44import fs from 'fs'
55import type { UseState } from './types'
66
7- export function randomColor (
8- palette :any ,
9- setPalette : UseState < paletteT > ,
10- option : OptionValue
11- ) : NodeJS . Timeout {
12- const colorInterval = setInterval ( ( ) => {
13- const newColors : BaseHexColor [ ] = [ ]
14- for ( let i = 0 ; i < palette . size ; i ++ ) {
15- let color = new HexColor ( )
7+ export function randomColor ( palette : any , setPalette : UseState < paletteT > , option : OptionValue ) : NodeJS . Timeout {
8+ const colorInterval = setInterval ( ( ) => {
9+ const newColors : BaseHexColor [ ] = [ ]
10+ for ( let i = 0 ; i < palette . size ; i ++ ) {
11+ let color = new HexColor ( )
1612
17- if ( option == 'cold' ) color = new HexColorCold ( )
18- else if ( option == 'pastel' ) color = new HexColorPastele ( )
19- else if ( option == 'warm' ) color = new HexColorWarm ( )
13+ if ( option == 'cold' ) color = new HexColorCold ( )
14+ else if ( option == 'pastel' ) color = new HexColorPastele ( )
15+ else if ( option == 'warm' ) color = new HexColorWarm ( )
2016
21- newColors . push ( color )
22- if ( newColors . length > 1 ) {
23- color . correctChanels ( newColors [ i - 1 ] ! )
24- }
25- }
26- setPalette ( ( p ) => ( { ...p , colors : [ ...p . colors , newColors ] } ) )
27- setPalette ( ( p ) => ( { ...p , position : p . position + 1 } ) )
28- } , 300 )
29- return colorInterval
17+ newColors . push ( color )
18+ if ( newColors . length > 1 ) {
19+ color . correctChanels ( newColors [ i - 1 ] ! )
20+ }
21+ }
22+ setPalette ( ( p ) => ( { ...p , colors : [ ...p . colors , newColors ] } ) )
23+ setPalette ( ( p ) => ( { ...p , position : p . position + 1 } ) )
24+ } , 300 )
25+ return colorInterval
3026}
3127
3228export function savePaletteWrapedC ( palette : BaseHexColor [ ] , countColorsPalette : number ) : number {
33- const { canvasSo } = require ( './c/canvas.ts' )
34- const libData = Buffer . from ( canvasSo , 'base64' )
35- if ( ! fs . existsSync ( '/tmp/canvas_lib.so' ) )
36- fs . writeFileSync ( '/tmp/canvas_lib.so' , libData , { encoding : 'binary' , mode : 0o775 } )
37- const cLib = dlopen ( '/tmp/canvas_lib.so' , {
38- save_palette : {
39- returns : 'int' ,
40- args : [ 'int' , 'pointer' , 'pointer' ] ,
41- } ,
42- } )
43- const paletteHex : ArrayLike < string > [ ] = [ ]
44- palette . forEach ( ( hex ) => paletteHex . push ( hex . get ( ) . replace ( '#' , '' ) ) )
45- const buffersPalette = paletteHex . map ( ( str ) => {
46- const buf = new Uint8Array ( Buffer . from ( str + '\0' ) )
47- return buf
48- } )
49- const pointerPalette = buffersPalette . map ( ( buf ) => ptr ( buf ) )
50- const palettePtr = ptr ( new BigUint64Array ( pointerPalette . map ( ( p ) => BigInt ( p ) ) ) )
29+ const { canvasSo } = require ( './c/canvas.ts' )
30+ const libData = Buffer . from ( canvasSo , 'base64' )
31+ if ( ! fs . existsSync ( '/tmp/canvas_lib.so' ) )
32+ fs . writeFileSync ( '/tmp/canvas_lib.so' , libData , { encoding : 'binary' , mode : 0o775 } )
33+ const cLib = dlopen ( '/tmp/canvas_lib.so' , {
34+ save_palette : {
35+ returns : 'int' ,
36+ args : [ 'int' , 'pointer' , 'pointer' ] ,
37+ } ,
38+ } )
39+ const paletteHex : ArrayLike < string > [ ] = [ ]
40+ palette . forEach ( ( hex ) => paletteHex . push ( hex . get ( ) . replace ( '#' , '' ) ) )
41+ const buffersPalette = paletteHex . map ( ( str ) => {
42+ const buf = new Uint8Array ( Buffer . from ( str + '\0' ) )
43+ return buf
44+ } )
45+ const pointerPalette = buffersPalette . map ( ( buf ) => ptr ( buf ) )
46+ const palettePtr = ptr ( new BigUint64Array ( pointerPalette . map ( ( p ) => BigInt ( p ) ) ) )
5147
52- const textColors : ArrayLike < string > [ ] = [ ]
53- palette . forEach ( ( color ) => textColors . push ( color . textColor . replace ( '#' , '' ) ) )
54- const buffersTextColors = textColors . map ( ( str ) => {
55- const buf = new Uint8Array ( Buffer . from ( str + '\0' ) )
56- return buf
57- } )
58- const pointerTextColors = buffersTextColors . map ( ( buf ) => ptr ( buf ) )
59- const textColorPtr = ptr ( new BigUint64Array ( pointerTextColors . map ( ( p ) => BigInt ( p ) ) ) )
48+ const textColors : ArrayLike < string > [ ] = [ ]
49+ palette . forEach ( ( color ) => textColors . push ( color . textColor . replace ( '#' , '' ) ) )
50+ const buffersTextColors = textColors . map ( ( str ) => {
51+ const buf = new Uint8Array ( Buffer . from ( str + '\0' ) )
52+ return buf
53+ } )
54+ const pointerTextColors = buffersTextColors . map ( ( buf ) => ptr ( buf ) )
55+ const textColorPtr = ptr ( new BigUint64Array ( pointerTextColors . map ( ( p ) => BigInt ( p ) ) ) )
6056
61- cLib . symbols . save_palette ( countColorsPalette , palettePtr , textColorPtr )
62- return 1
57+ cLib . symbols . save_palette ( countColorsPalette , palettePtr , textColorPtr )
58+ return 1
6359}
0 commit comments