[HELPER] Table Color For Pairing Black And White
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('[HELPER] Table Color For Pairing Black And White', shorttitle = 'TCFPAB')

// 0. Inputs
// 1. Variables and arrays
// 2. Custom functions
// 3. Plot
// 4. Construct

// 0. Inputs {
T1       = 'Small font size recommended for mobile app or multiple layout'
i_b_col  = input.bool(false, 'Invert Color')
i_s_Y    = input.string('middle', 'Table Position', inline = '1', options = ['top',  'middle', 'bottom'])
i_s_X    = input.string('center',               '', inline = '1', options = ['left', 'center', 'right'])
i_s_font = input.string('normal',      'Font size', tooltip = T1, options = ['tiny', 'small',  'normal', 'large', 'huge'])
// }
// 1. Variables and arrays {
var colorTable = table.new(i_s_Y + '_' + i_s_X, 3, 16, border_width = 1)
var label_color = array.new_label(16)
//                               0           1              2             3             4             5            6             7          8           9
arr_black = array.from( color.aqua, color.lime,  color.orange, color.silver,  color.white, color.yellow)
arr_white = array.from(color.black, color.blue, color.fuchsia,  color.green, color.maroon,   color.navy, color.olive, color.purple, color.red, color.teal)
//                            1             2           3              4            5            6             7           8            9            10            11
str_col = array.from(    'aqua',      'black',     'blue',     'fuchsia',      'green',     'lime',     'maroon',     'navy',     'olive',     'orange',     'purple',
                          'red',     'silver',     'teal',       'white',     'yellow')
arr_col = array.from(color.aqua,  color.black, color.blue, color.fuchsia,  color.green, color.lime, color.maroon, color.navy, color.olive, color.orange, color.purple,
                      color.red, color.silver, color.teal,   color.white, color.yellow)
// }
// 2. Custom functions {
// 2.1 Plot
p_col(int _index)   => array.get(arr_col, _index)

// 2.2 Cell table
_bgcolor(int _index) =>
    color col = switch i_b_col
        true  => array.includes(arr_black, array.get(arr_col, _index)) ? color.black : array.includes(arr_white, array.get(arr_col, _index)) ? color.white : na
        false => array.get(arr_col, _index)

_color(int _index) =>
    color col = switch i_b_col
        true  => array.get(arr_col, _index)
        false => array.includes(arr_black, array.get(arr_col, _index)) ? color.black : array.includes(arr_white, array.get(arr_col, _index)) ? color.white : na

str(int _index) =>
    array.includes(arr_black,  array.get(arr_col, _index)) ? 'color.black' :
     array.includes(arr_white, array.get(arr_col, _index)) ? 'color.white' : ''

f_cell(int _column, int _row, string _text) =>
    table.cell(colorTable, _column, _row, _text, text_color = _color(_row), text_size = i_s_font, bgcolor =  _bgcolor(_row))

f_row(int _row) =>
    f_cell(0, _row, str.tostring(_row))
    f_cell(1, _row, array.get(str_col, _row))
    f_cell(2, _row, str(_row))

// 2.3 Label
f_label(int _index) =>
    array.set(label_color, _index,
     label.new(bar_index, -_index, array.get(str_col, _index), color = _bgcolor(_index), style = label.style_label_left, size = i_s_font, textcolor = _color(_index))
     )
// }
// 3. Plot {
plot(  0,    'aqua', p_col(0))
plot( -1,   'black', p_col(1))
plot( -2,    'blue', p_col(2))
plot( -3, 'fuchsia', p_col(3))
plot( -4,   'green', p_col(4))
plot( -5,    'lime', p_col(5))
plot( -6,  'maroon', p_col(6))
plot( -7,    'navy', p_col(7))
plot( -8,   'olive', p_col(8))
plot( -9,  'orange', p_col(9))
plot(-10,  'purple', p_col(10))
plot(-11,     'red', p_col(11))
plot(-12,  'silver', p_col(12))
plot(-13,    'teal', p_col(13))
plot(-14,   'white', p_col(14))
plot(-15,  'yellow', p_col(15))
// }
// 4. Construct {
if barstate.islast
    for x = 0 to 15
        f_row(x)
        f_label(x)
// }