NB. name that color (displays color table)
NB. showcolor   - show colors

require 'colortab'
require 'gl2'
require jpath '~system\packages\color\rgb.ijs'

cocurrent 'z'

NB. =========================================================
NB. base form
NTC=: 0 : 0
pc ntc closeok;pn "ntcolor";
xywh 6 7 166 150;cc clr listbox ws_vscroll rightscale bottommove;
xywh 183 6 49 62;cc g0 groupbox leftscale rightscale;cn "Sort By";
xywh 189 16 40 9;cc name radiobutton leftscale rightscale;cn "name";
xywh 189 26 40 9;cc red radiobutton group leftscale rightscale;cn "red";
xywh 189 36 40 9;cc green radiobutton group leftscale rightscale;cn "green";
xywh 189 46 40 9;cc blue radiobutton group leftscale rightscale;cn "blue";
xywh 189 56 40 9;cc total radiobutton group leftscale rightscale;cn "total";
xywh 243 6 34 11;cc cancel button leftmove rightmove;cn "Close";
xywh 181 72 50 9;cc scale static leftscale rightmove;cn "Nearest integer";
xywh 243 69 34 11;cc sixes button leftmove rightmove;cn "Scale";
xywh 181 84 97 64;cc gcolors isigraph leftscale rightmove bottommove;
pas 4 4;pcenter;
rem form end;
)

NB. =========================================================
ntc_run=: 3 : 0
if. 0 e. $y. do.
  dat=. COLORTABLE
else.
  dat=. y.
  if. 2 ~: 3!:0 dat do.
    f=. }.@":@(1000&+)
    dat=. (>'color'&, @ (,&'=:  ') @ f each i.#dat),.":dat
  end.
end.
SIXES=: 0
ndx=. dat i. "1 '='
COLORNMS=: (20 >. 2+>./ndx) {."1 ndx {."0 1 dat
COLORVAL=: ".@(}.~ [: >: i.&':')"1 dat
COLORVORG=: COLORVAL
COLORNDX=: i.#dat
j=. FIXFONT
font=. j,(0 e. #j)#'Terminal 9 oem'
wd NTC
glmap_jgl2_ MM_RAW_jgl2_
wd 'setfont clr ',font
wd 'set clr *',,(COLORNMS,.":COLORVAL),.LF
wd 'setfocus clr'
wd 'setselect clr 0'
if. IFWINCE do. wdfit 1 end.
ntc_color 0
wd 'pshow;'
)

NB. =========================================================
ntc_cancel_button=: wd bind 'pclose'

NB. =========================================================
ntc_color=: 3 : 0
clr=. (y. { COLORNDX) { COLORVAL
glclear_jgl2_''
glrgb_jgl2_ clr
glbrush_jgl2_''
glrect_jgl2_ 0 0 1000 1000
glshow_jgl2_''
glpaint_jgl2_''
)

NB. =========================================================
ntc_clr_button=: 3 : 0
ntc_color ".clr_select
)

NB. =========================================================
ntc_clr_select=: ntc_enter=: ntc_clr_button

NB. =========================================================
ntc_show=: 3 : 0
0 ntc_show y.
:
wd 'set clr *',,(COLORNDX{COLORNMS,.":COLORVAL),.LF
wd 'setselect clr ',":x.
ntc_color x.
)

NB. =========================================================
ntc_name_button=: 3 : 0
ntc_show COLORNDX=: /: COLORNMS
)

NB. =========================================================
ntc_red_button=: 3 : 0
ntc_show COLORNDX=: \: COLORVAL
)

NB. =========================================================
ntc_green_button=: 3 : 0
ntc_show COLORNDX=: \: 1 0 2{"1 COLORVAL
)

NB. =========================================================
ntc_blue_button=: 3 : 0
ntc_show COLORNDX=: \: 2 0 1{"1 COLORVAL
)

NB. =========================================================
NB. by total, reds first
ntc_total_button=: 3 : 0
ntc_show COLORNDX=: \: (+/"1 COLORVAL),.COLORVAL
)

NB. =========================================================
ntc_sixes_button=: 3 : 0
SIXES=: 5 | >: SIXES
if. SIXES e. 1 2 3 4 do.
  if. SIXES e. 1 2 4 do.
NB.   n=. 8 * 2 ^ SIXES
    n=. 16 32 64 {~ 1 2 4 i. SIXES
  else.
    n=. 51
  end.
  COLORVAL=: 255 <. n * <. 0.5 + COLORVORG % n
else.
  COLORVAL=: COLORVORG
end.
wd 'set scale *Nearest ',SIXES pick 'integer';'16';'32';'51';'64'
(".clr_select) ntc_show ''
)

NB. =========================================================
ntcolor=: ntc_run

NB. =========================================================
NB. showcolor    - show colors
NB.
NB. showcolor colormatrix
NB.
NB. colormatrix is a single RBG triplet, or 3 column matrix of RGB triplets
NB.
NB. e.g.  showcolor ?100 3$255
NB.       showcolor ".  COLORTABLE
showcolor=: 3 : 0

wd 'pc showcolor closeok'
wd 'xywh 0 0 200 150;cc g0 isigraph rightmove bottommove;'
wd 'pas 0 0;pcenter;ptop;'

y=. (_2{.1,$y.)$,y.
num=. #y
rws=. <. %: num
cls=. >. num % rws

irws=. 1000 % rws
icls=. 1000 % cls

xy=. > , |: { (irws * i.rws);icls * i.cls
rect=. num {. xy ,"1 irws,icls
rect=. (1000 - icls + 1 {"1 rect) 1 }"0 1 rect

cmd=. glrect_jgl2_@[ glbrush_jgl2_@glrgb_jgl2_
0 0 1000 1000 cmd 128 128 128
rect cmd"1 y
glshow_jgl2_''

wd 'pshow'
)
