11import { createCell } from "../../model/cell" ;
2- import { AnalyzedImage , Cell , Color , Draft , DynamicOperation , FileParam , NumParam , OperationInlet , OpInput , OpParamVal } from "../../model/datatypes" ;
3- import { getHeddle , initDraftFromDrawdown , initDraftWithParams , warps , wefts } from "../../model/drafts" ;
2+ import { AnalyzedImage , Cell , Draft , DynamicOperation , FileParam , NumParam , OperationInlet , OpInput , OpParamVal } from "../../model/datatypes" ;
3+ import { createDraft , getHeddle , initDraftFromDrawdown , initDraftWithParams , warps , wefts } from "../../model/drafts" ;
44import { getOpParamValById } from "../../model/operations" ;
55
66
@@ -61,21 +61,17 @@ const perform = (op_params: Array<OpParamVal>, op_inputs: Array<OpInput>) => {
6161
6262 const data :AnalyzedImage = file_param . data ;
6363
64- const color_to_drafts = data . colors_mapping . map ( ( color , ndx ) => {
65-
66- let color_to :Color = data . colors [ color . to ] ;
67-
64+
65+ const color_to_drafts = data . colors . map ( ( color , ndx ) => {
66+ const child_of_color = op_inputs . find ( input => ( input . params . findIndex ( param => param === color . hex ) !== - 1 ) ) ;
6867
69- if ( color_to . hex == '#000000' ) {
70- if ( op_inputs . findIndex ( input => input . inlet_id == 0 ) !== - 1 ) return { color : color_to . hex , draft : op_inputs [ 0 ] . drafts [ 0 ] }
71- else return { color : color_to . hex , draft : initDraftWithParams ( { warps : 1 , wefts : 1 , drawdown : [ [ createCell ( true ) ] ] } ) }
72- } else {
73- if ( op_inputs . findIndex ( input => input . inlet_id == 1 ) !== - 1 ) return { color : color_to . hex , draft : op_inputs [ 1 ] . drafts [ 0 ] }
74- else return { color : color_to . hex , draft : initDraftWithParams ( { warps : 1 , wefts : 1 , drawdown : [ [ createCell ( false ) ] ] } ) }
68+ if ( child_of_color === undefined ) {
69+ if ( color . hex == '#000000' ) return { color : color . hex , draft : initDraftWithParams ( { warps : 1 , wefts : 1 , drawdown : [ [ createCell ( true ) ] ] } ) } ;
70+ else return { color : color . hex , draft : initDraftWithParams ( { warps : 1 , wefts :1 , drawdown : [ [ createCell ( false ) ] ] } ) } ; ;
71+ }
72+ else return { color : color . hex , draft : child_of_color . drafts [ 0 ] } ;
73+ } ) ;
7574
76- }
77- } ) ;
78-
7975
8076 const pattern : Array < Array < Cell > > = [ ] ;
8177 for ( let i = 0 ; i < res_h ; i ++ ) {
@@ -88,8 +84,13 @@ const perform = (op_params: Array<OpParamVal>, op_inputs: Array<OpInput>) => {
8884 const map_i = Math . floor ( i * i_ratio ) ;
8985 const map_j = Math . floor ( j * j_ratio ) ;
9086
91- const color_ndx = data . image_map [ map_i ] [ map_j ] ; //
92- const color_draft = color_to_drafts [ color_ndx ] . draft ;
87+ const color_ndx = data . image_map [ map_i ] [ map_j ] ;
88+ const mapped_color = data . colors_mapping . find ( el => el . from == color_ndx ) ; //this is the mapped id
89+ const mapped_color_hex = data . colors [ mapped_color . to ] . hex
90+ const color_draft = color_to_drafts . find ( el => el . color == mapped_color_hex ) . draft ;
91+
92+
93+
9394
9495 if ( color_draft === null ) pattern [ i ] . push ( createCell ( false ) ) ;
9596 else {
@@ -120,11 +121,44 @@ const generateName = (param_vals: Array<OpParamVal>, op_inputs: Array<OpInput>)
120121
121122const onParamChange = ( param_vals : Array < OpParamVal > , inlets : Array < OperationInlet > , inlet_vals : Array < any > , changed_param_id : number , param_val : any ) : Array < any > => {
122123
124+ //go through the image and adjust the mapping data so that it adds black and white and maps to those.
123125 const new_inlets : Array < any > = [ '#000000' , '#ffffff' ]
126+ let img = param_val . data ;
127+ console . log ( "IMG" , img ) ;
128+
129+ if ( img == undefined ) return new_inlets ;
130+
131+
132+
133+ let has_black = false ;
134+ let has_white = false ;
135+
136+ img . colors . forEach ( color => {
137+ if ( color . hex == '#000000' ) has_black = true ;
138+ if ( color . hex == '#ffffff' ) has_white = true ;
139+ } )
140+
141+ if ( ! has_black ) img . colors . push ( { r : 0 , g : 0 , b : 0 , hex : '#000000' , black : true } )
142+ if ( ! has_white ) img . colors . push ( { r : 255 , g : 255 , b : 255 , hex : '#ffffff' , black : false } )
143+
144+ let black_id = img . colors . findIndex ( el => el . hex == '#000000' ) ;
145+ let white_id = img . colors . findIndex ( el => el . hex == '#ffffff' ) ;
146+
147+
148+ img . colors_mapping . forEach ( mapping => {
149+ let c = img . colors [ mapping . from ] ;
150+ if ( c . black ) mapping . to = black_id ;
151+ else mapping . to = white_id ;
152+ } )
124153
125154 return new_inlets ;
126155
127156
157+
158+
159+
160+
161+
128162}
129163
130164
0 commit comments