matrixautotable
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kaigouthro
//@version=5
import kaigouthro/hsvColor/15 as h
import kaigouthro/font/4
import kaigouthro/into/2

// @description Automatic Table from Matrixes with pseudo correction for na values and default color override for missing
library("matrixautotable")

// Grower (soon to be a lib)
maxcol(a,b,c,d,e) => math.max( matrix.columns(a),  matrix.columns(b),  matrix.columns(c),  matrix.columns(d),  matrix.columns(e) )
maxrow(a,b,c,d,e) => math.max( matrix.rows   (a),  matrix.rows   (b),  matrix.rows   (c),  matrix.rows   (d),  matrix.rows   (e) )

growcol(sz,m)=>
    _c = matrix.columns(m)
    if _c < sz
        while _c < sz
            matrix.add_col(m,_c)
            _c:=matrix.columns(m)

growrow(sz,m)=>
    _r = matrix.rows(m)
    if _r < sz
        while _r < sz
            matrix.add_row(m,_r)
            _r:=matrix.rows(m)

// quick replacers
mst (_y,_x) => matrix.new<string>(_y,_x)
mfl (_y,_x) => matrix.new<float>(_y,_x)
mcl (_y,_x) => matrix.new<color>(_y,_x)
grow(a,b,c,d,e)          =>
    var _r =  maxrow (a,b,c,d,e)
    var _c =  maxcol (a,b,c,d,e)
    _c    :=  maxcol (a,b,c,d,e)
    _r    :=  maxrow (a,b,c,d,e)
    
    growcol(_c,a)
    growcol(_c,b)
    growcol(_c,c)
    growcol(_c,d)
    growcol(_c,e)
    
    growrow(_r,a)
    growrow(_r,b)
    growrow(_r,c)
    growrow(_r,d)
    growrow(_r,e)



invertcol(color COL)=>
    h.tripswitch(COL,0.8,#ffffff,#333333)

sizech(_mtx) => _cl = matrix.columns (_mtx) , _rw = matrix.rows(_mtx),  [_rw,_cl]

export type cellsize
    float w
    float h

// @function    autogenerate table from string matrix
mtable( _datamatrix, matrix<string> _stringmatrix, matrix<color>  _bgcolormatrix, matrix<color>  _textcolormatrix, matrix<string> _tooltipmatrix,  int _textSizeinp = 1, string tableYpos = 'bottom', string  tableXpos = 'left', bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #86868680,_cellsize)=> //{     
    [_rwf,_clf] = sizech(_datamatrix) ,    [_rws,_cls] = sizech(_stringmatrix) ,    [_rwt,_clt] = sizech(_tooltipmatrix) ,    [_rwb,_clb] = sizech(_bgcolormatrix) ,    [_rwtt,_cltt] = sizech(_textcolormatrix) 
    var table _table = na
    varip _rsz = 0
    varip _csz = 0
    _rsz   := math.max(_rwf,_rws,_rwt,_rwb,_rwtt)
    _csz   := math.max(_clf,_cls,_clt,_clb,_cltt)
    if bar_index +3 >= last_bar_index
        _table := table.new (tableYpos  + '_' + tableXpos, _csz, _rsz, na, na, 1, na, 1) 
    _wid = _fit ? 100/_csz:_shrink/_csz*_cellsize.w
    _hgt = _fit ? 100/_rsz:_shrink/_rsz*_cellsize.h
    var _xsize   = int(na),      var _ysize   = int(na)
    var _textSize  = switch _textSizeinp
        1 => size.tiny    
        2 => size.small   
        3 => size.normal  
        4 => size.large  
        5 => size.huge
        => size.auto    
    if bar_index > last_bar_index - 3
        _xsize   := _csz 
        _ysize   := _rsz 
        var color  _nocol     = color.new(color.black,100)
        for _currClm = 0  to _xsize  -1  by 1
            for _currRow  = 0 to _ysize  -1 by 1
                _value       = matrix.get(_datamatrix,_currRow,_currClm)
                _string      = matrix.get(_stringmatrix,_currRow,_currClm)
                _valstr      = str.replace(na(_value) ? '' : (na(_string) ? ' ' : '\n') + into.s(_value), 'NaN','')
                _cellcolor   = matrix.get(_bgcolormatrix   , _currRow , _currClm) 
                _cellcolor  := na(_cellcolor)  ? _defaultbg  : _cellcolor 
                _textcolor   = matrix.get(_textcolormatrix , _currRow , _currClm)
                _textcolor  := (_defaultxt == _textcolor) or na(_textcolor) ? invertcol(_cellcolor) : _textcolor
                _string     := (na(_string) ? ' ' : _string ) + _valstr
                _tooltip    = matrix.get(_tooltipmatrix   , _currRow , _currClm) 
                _tooltip    := na(_tooltip  ) ? ' ' : _tooltip
                table.cell(_table, _currClm, _currRow , _string, _wid,_hgt , _textcolor , text.align_center , text.align_center , _textSize , _cellcolor , _tooltip )    
        _table

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _bgcolormatrix       <matrix> color
// @param       _textcolormatrix     <matrix>color
// @param       _tooltipmatrix       <matrix> string
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<float>  _floatmatrix, matrix<string> _stringmatrix, matrix<color>  _bgcolormatrix, 
                                  matrix<color>  _textcolormatrix,  matrix<string> _tooltipmatrix,  
                                  int _textSize = 2, string tableYpos = 'bofloat _w=1,float _h=1, ttom',  string tableXpos = 'left' , 
                                  bool _fit = false, int _shrink = 0, float _w=1,float _h=1, 
                                  color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)       =>        //{
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h)) 

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _bgcolormatrix       <matrix> color
// @param       _textcolormatrix     <matrix>color
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<float>  _floatmatrix,  matrix<string> _stringmatrix, matrix<color>  _bgcolormatrix, matrix<color>  _textcolormatrix,                                   
          int _textSize    = 2, string tableYpos = 'bottom',      string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                             =>       //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    var matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _bgcolormatrix       <matrix> color
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<float>  _floatmatrix,  matrix<string> _stringmatrix, matrix<color>  _bgcolormatrix,                                                                       
          int _textSize    = 2, string tableYpos = 'bottom',   string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                          =>    //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    var matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    var matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<float>  _floatmatrix,  matrix<string> _stringmatrix,                                                                                                      
          int _textSize    = 2, string tableYpos = 'bottom',   string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                          =>    //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    var matrix<color>  _bgcolormatrix   = matrix.new<color>  (_sizey, _sizex,  _defaultbg  )
    var matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    var matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _bgcolormatrix       <matrix> color
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<string>  _floatmatrix,  matrix<string> _stringmatrix, matrix<color>  _bgcolormatrix,                                                                       
          int _textSize    = 2, string tableYpos = 'bottom',   string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                          =>    //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _stringmatrix        <matrix> string
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<string>  _floatmatrix,  matrix<string> _stringmatrix,                                                                                                      
          int _textSize    = 2, string tableYpos = 'bottom',   string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                          =>    //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    matrix<color>  _bgcolormatrix   = matrix.new<color>  (_sizey, _sizex,  _defaultbg  )
    matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix, _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _floatmatrix        <matrix> float vals
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<float>  _floatmatrix,                                                                                                                                     
          int _textSize    = 2, string tableYpos = 'bottom',   string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                          =>    //{
    var _sizey = int(na), _sizey := matrix.rows(_floatmatrix), var _sizex = int(na), _sizex := matrix.columns(_floatmatrix)
    matrix<string>  _stringmatrix    = matrix.new<string> (_sizey, _sizex,  string(na) )
    matrix<color>  _bgcolormatrix   = matrix.new<color>  (_sizey, _sizex,  _defaultbg  )
    matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix    , _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix    , _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))

// @function    matrixtable          
// @param       _stringmatrix        <matrix> float vals
// @param       _textSize            int
// @param       tableYpos            string
// @param       tableXpos            string
export matrixtable(matrix<string>  _stringmatrix,                                                                                                                                     
          int _textSize    = 2, string tableYpos = 'bottom', string tableXpos = 'left'            ,
          bool _fit = false, int _shrink = 0, float _w=1,float _h=1, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41)                        =>  //{
    var _sizey = int(na), _sizey := matrix.rows(_stringmatrix), var _sizex = int(na), _sizex:= matrix.columns(_stringmatrix)
    matrix<float>  _floatmatrix     = matrix.new<float>  (_sizey, _sizex,  float(na) )
    matrix<color>  _bgcolormatrix   = matrix.new<color>  (_sizey, _sizex,  _defaultbg  )
    matrix<color>  _textcolormatrix = matrix.new<color>  (_sizey, _sizex,  _defaultxt  )
    matrix<string> _tooltipmatrix   = matrix.new<string> (_sizey, _sizex,  string(na) )
    grow  (_floatmatrix, _stringmatrix    , _bgcolormatrix, _textcolormatrix, _tooltipmatrix)
    mtable(_floatmatrix, _stringmatrix    , _bgcolormatrix, _textcolormatrix, _tooltipmatrix, _textSize   ,tableYpos   ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt,cellsize.new(_w,_h))



export anytwo( matrix<float> _titleMTX, matrix<float> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<int> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<string> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<bool> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<float> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<int> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<string> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<bool> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<float> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<int> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<string> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<bool> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<float> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<int> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<string> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<bool> _dataMTX , matrix<color> _bgcolormatrix, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _bgcolormatrix, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<float> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<int> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<string> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<float> _titleMTX, matrix<bool> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<float> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<int> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<string> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<int> _titleMTX, matrix<bool> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<float> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<int> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<string> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<string> _titleMTX, matrix<bool> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<float> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<int> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<string> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anytwo( matrix<bool> _titleMTX, matrix<bool> _dataMTX , int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = into.s(_dataMTX), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)

export anyone( matrix<float>  _titleMTX, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = matrix.new<string>(matrix.rows(_titleMTX),matrix.columns(_titleMTX),string(na)), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anyone( matrix<int>    _titleMTX, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = matrix.new<string>(matrix.rows(_titleMTX),matrix.columns(_titleMTX),string(na)), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anyone( matrix<string> _titleMTX, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = matrix.new<string>(matrix.rows(_titleMTX),matrix.columns(_titleMTX),string(na)), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anyone( matrix<bool> _titleMTX, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = into.s(_titleMTX),_d = matrix.new<string>(matrix.rows(_titleMTX),matrix.columns(_titleMTX),string(na)), matrixtable(_d,_t, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)
export anyone( matrix<color> _colormtx, int _textSize = 2, string tableYpos = 'bottom', string tableXpos = 'left' , bool _fit = false, int _shrink = 0, float _w = 1, float _h = 2, color _defaultbg = #101010ff, color _defaultxt = #fdfdfd41) => _t = matrix.new<string>(matrix.rows(_colormtx),matrix.columns(_colormtx),string(na)), matrixtable(_t,_t,_colormtx, _textSize ,tableYpos ,tableXpos, _fit, _shrink,_w,_h, _defaultbg, _defaultxt)



 
// wrapped shortcuts for table post-adjusstments

// @function Set background or Table
// @param   bgcolor New Background Color
// @returns  void
export bgcolor (table _table, color  bgcolor  ) =>
    table.set_bgcolor (_table, bgcolor  )

// @function TODO: add function description here
// @param   border_color New border color
// @param   border_width New border width
// @returns void
export border  (table _table, color  border_color ,int border_width  ) =>
    table.set_border_color(_table,   border_color ) 
    table.set_border_width(_table,   border_width ) 
export border  (table _table, int    border_width )  =>
    table.set_border_width(_table,   border_width ) 
export border  (table _table, color  border_color )  =>
    table.set_border_color(_table,   border_color ) 

// @function Alter frame
// @param    New border color
// @param    New border width
// @returns  void
export frame   (table _table, color  frame_color  ,int frame_width   ) =>
    table.set_frame_color (_table, frame_color  )
    table.set_frame_width (_table, frame_width  )
export frame   (table _table, int  frame_width  ) =>
    table.set_frame_width (_table, frame_width  )
export frame   (table _table, color  frame_color) =>
    table.set_frame_color (_table, frame_color  )


// // demo 
var string  group_table    = ' Table'
int        _tblssizedemo  = input.int    ( 10 )
string      tableYpos      = input.string ( 'middle'    , '↕'               , inline = 'place' , group = group_table, options=['top', 'middle', 'bottom'])
string      tableXpos      = input.string ( 'center'    , '↔'               , inline = 'place' , group = group_table, options=['left', 'center', 'right'], tooltip='Position on the chart.')
int         _textSize      = input.int    ( 1           , 'Table Text Size' , inline = 'place' , group = group_table)

// fulltable
matrix<float>  _floatmatrix      = matrix.new<float>  (_tblssizedemo, _tblssizedemo,  0          ) 
matrix<string> _stringmatrix     = matrix.new<string> (_tblssizedemo, _tblssizedemo,  ' '        ) 
matrix<color>  _bgcolormatrix    = matrix.new<color>  (_tblssizedemo, _tblssizedemo,  color(na)  ) 
matrix<color>  _textcolormatrix  = matrix.new<color>  (_tblssizedemo, _tblssizedemo,  na         )
matrix<string> _tooltipmatrix    = matrix.new<string> (_tblssizedemo, _tblssizedemo,  'tooltip'  ) 


////   for demo purpose, random colors, random nums, random na vals

_ysize = matrix.rows    (_floatmatrix) 
_xsize = matrix.columns (_floatmatrix) 


if bar_index + 10 >= last_bar_index
    for _currClm = 0  to _xsize  -1  by 1
        for _currRow  = 0 to _ysize  -1 by 1
            _randomh = math.random(0,360)
            _randoms = math.random(.6,1.)
            _randomv = math.random(.25,1)
            _randoma = math.random(.8,1)
            bgcolor  = h.hsv(_randomh,_randoms,_randomv,_randoma)
            txtcolor = h.hsvInvert(bgcolor)
            matrix.set(_bgcolormatrix    ,_currRow,_currClm, bgcolor )
            matrix.set(_textcolormatrix  ,_currRow,_currClm, txtcolor)
            matrix.set(_floatmatrix      ,_currRow,_currClm, math.round(_randomh,2))
            
            // random na
            _ymiss = math.floor(math.random(0, _currRow))
            _xmiss = math.floor(math.random(0, _currClm))
            matrix.set( _floatmatrix     ,_ymiss,  _currClm, na)
            matrix.set( _stringmatrix    ,_ymiss,  _currClm, na)
            matrix.set( _bgcolormatrix   ,_ymiss,  _currClm, na)
            matrix.set( _textcolormatrix ,_ymiss,  _currClm, na)
            matrix.set( _tooltipmatrix   ,_ymiss,  _currClm, na)


_defaultbg = input.color(color.gray)
_defaultxt = input.color(color.black)

_fit = input(true)
_cellsize = input(2)

if barstate.islast
    _table = switch input(4,'table')
        1 => matrixtable(_floatmatrix,  _stringmatrix,  _bgcolormatrix, _textcolormatrix,   _tooltipmatrix , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize )
        2 => matrixtable(_floatmatrix,  _stringmatrix,  _bgcolormatrix, _textcolormatrix,   _tooltipmatrix , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        3 => matrixtable(_floatmatrix,  _stringmatrix,  _bgcolormatrix, _textcolormatrix                   , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        4 => matrixtable(_floatmatrix,  _stringmatrix,  _bgcolormatrix                                     , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        5 => matrixtable(_floatmatrix,  _stringmatrix                                                      , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        6 => matrixtable(_floatmatrix                                                                      , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        7 => matrixtable(_stringmatrix                                                                     , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
        8 => anyone     (_bgcolormatrix                                                                    , _textSize   ,tableYpos   ,tableXpos , _fit, _cellsize ) 
    border  ( _table,1)
    border  ( _table ,h.hsv((timenow/200)%360,1,1,.05))
