RGB Color Codes Chart
// 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('RGB Color Codes Chart', 'RCCC', true)

// 0. Inputs
// 1. Types
// 2. Custom Functions
// 3. Arrays and Variables
// 4. Constructs

//#region ———————————————————— 0. Inputs
Tooltip0     = 'Small font size recommended for mobile app or multiple layout'
Title0       = 'Show One color only instead Gradient Color'
Title1       = 'Value Reference'
titleRed     = input.bool(     true,           'Red', group = 'Color',  inline = '0')
titleOrange  = input.bool(     true,        'Orange', group = 'Color',  inline = '0')
titleYellow  = input.bool(     true,        'Yellow', group = 'Color',  inline = '0')
titleLime    = input.bool(     true,          'Lime', group = 'Color',  inline = '0')
titleGreen   = input.bool(     true,         'Green', group = 'Color',  inline = '1')
titleTeal    = input.bool(     true,          'Teal', group = 'Color',  inline = '1')
titleAqua    = input.bool(     true,          'Aqua', group = 'Color',  inline = '1')
titleBlue    = input.bool(     true,          'Blue', group = 'Color',  inline = '1')
titleNavy    = input.bool(     true,          'Navy', group = 'Color',  inline = '2')
titlePurple  = input.bool(     true,        'Purple', group = 'Color',  inline = '2')
titleMaroon  = input.bool(     true,        'Maroon', group = 'Color',  inline = '2')
titleFuchsia = input.bool(     true,       'Fuchsia', group = 'Color',  inline = '3')
titleGray    = input.bool(     true,          'Gray', group = 'Color',  inline = '3')
showTitle    = input.bool(     true,    'Show Title', group = 'Others')
showString   = input.string(  'RGB', 'Select Values', group = 'Others', options = ['Full', 'HEX', 'RGB', 'R', 'G', 'B', 'na'])
fontSize     = input.string('small',     'Font Size', group = 'Others', options = ['tiny',  'small', 'normal', 'large', 'huge'], tooltip = Tooltip0)
showGradient = input.bool(    false,          Title0, group = 'Others')
intGradient  = input.int(         8,          Title1, minval = 0, maxval = 8)
//#endregion

//#region ———————————————————— 1. Types
// @type    Used for int
// @field r Int value for red
// @field g Int value for green
// @field b Int value for blue
type rgb
    int r = na
    int g = na
    int b = na

// @type    Used for string
// @field quotient  String value for quotient 
// @field remainder String value for remainder
type pair
    string quotient  = na
    string remainder = na

// @type        Used for cell properties
// @field title String value for title
// @field color Color value for col
type properties
    bool  enable = na
    string title = na
    color  col   = na
//#endregion

//#region ———————————————————— 2. Custom Functions
// @function createQuotient
// @param    val
// @returns  hex
switchHex(int val = na) =>
    hex = switch val
        10 => 'A'
        11 => 'B'
        12 => 'C'
        13 => 'D'
        14 => 'E'
        15 => 'F'
        => str.tostring(val)

// @function createQuotient
// @param    dividend
// @returns  quotient
createQuotient( int dividend = na) => int(dividend / 16)

// @function createRemainder
// @param    dividend
// @returns  remainder
createRemainder(int dividend = na) => int(dividend % 16)

// @function doubleHex
// @param    dividend
// @returns  double
doubleHex(int dividend = na) =>
    quotient  = switchHex(createQuotient( dividend))
    remainder = switchHex(createRemainder(dividend))
    double = pair.new(quotient, remainder)
    double

// @function convertRGBtoHex
// @param    RGB
// @returns  hex
convertRGBtoHex(rgb RGB = na) =>
    hexR = doubleHex(RGB.r)
    hexG = doubleHex(RGB.g)
    hexB = doubleHex(RGB.b)
    hex  = '#' + hexR.quotient + hexR.remainder + hexG.quotient + hexG.remainder + hexB.quotient + hexB.remainder
    hex

// @function groupRGB
// @param    i0, i1, i2
// @returns  id
method groupRGB(array<int> i0 = na, array<int> i1 = na, array<int> i2 = na) =>
    id = array.new<rgb>(9)
    for i = 0 to 8
        id.set(i, rgb.new(i0.get(i), i1.get(i), i2.get(i)))
    id

// @function stringRGB
// @param    RGB
// @returns  id
method stringRGB(array<rgb> RGB = na) =>
    id = array.new<string>(9)
    for i = 0 to 8
        id.set(i, 'HEX : ' + convertRGBtoHex(RGB.get(i)) + '\nR : ' + str.tostring(RGB.get(i).r, '000') + '\nG : ' + str.tostring(RGB.get(i).g, '000') + '\nB : ' + str.tostring(RGB.get(i).b, '000'))
    id

// @function createRGB
// @param    id, index
// @returns  model
method createRGB(array<rgb> id = na, int index = na)=>
    r     = id.get(index).r
    g     = id.get(index).g
    b     = id.get(index).b
    model = color.rgb(r, g, b)
    model

// @function createColor
// @param    id
// @returns  col
method createColor(array<rgb> id = na) =>
    col   = array.new<color>(9)
    for i = 0 to 8
        col.set(i, id.createRGB(i))
    col

// @function createCell
// @param    id, column, i, str, bg, fg
// @returns  id.cell
method createCell(   table id = na, int column = na, int i = na,
                     string[] str = na, color[] bg = na, color[] fg = na)  =>
    id.cell(column, i + 1, '',
             bgcolor    = bg.get(i),
             text_size  = fontSize,
             text_color = fg.get(i),
             tooltip    = array.get(str, i))

// @function boolCell
// @param    show, id, column, i, str, bg, fg
// @returns  id.cell
method boolCell( array<properties> show = na, table id = na, int column = na,
                 int i = na, string[] str = na, color[] bg = na, color[] fg = na)  =>
    if show.get(column).enable
        id.createCell(column, i, str, bg, fg)

// @function boolCell
// @param    show, id, column, i, str, bg, fg
// @returns  id.cell
method setCellText(array<properties> show = na, table id = na, int column = na, int i = na, string[] str = na, array<rgb> RGB = na) =>
    disp = switch showString
        'Full' => array.get(str, i)
        'HEX'  => 'HEX : ' + convertRGBtoHex(RGB.get(i))
        'RGB'  => 'R : ' + str.tostring(RGB.get(i).r, '000') + '\nG : ' + str.tostring(RGB.get(i).g, '000') + '\nB : ' + str.tostring(RGB.get(i).b, '000')
        'R'    => 'R : ' + str.tostring(RGB.get(i).r, '000')
        'G'    => 'G : ' + str.tostring(RGB.get(i).g, '000')
        'B'    => 'B : ' + str.tostring(RGB.get(i).b, '000')
        'na'   => ' '
    if show.get(column).enable
        id.cell_set_text(column, i + 1, disp)
//#endregion

//#region ———————————————————— 3. Arrays and Variables
colorText = array.new<color>(9)
palette   = array.new<properties>(13)
var tbl   = table.new(position.middle_center, 13, 10, border_width = 1)

for i = 0 to 3
    colorText.set(i, color.white)
for i = 4 to 8
    colorText.set(i, color.black)

//                        0    1    2    3    4    5    6    7    8
redR       = array.from( 51, 102, 153, 204, 255, 255, 255, 255, 255)
redG       = array.from(  0,   0,   0,   0,   0,  51, 102, 153, 204)
orangeG    = array.from( 25,  51,  76, 102, 128, 153, 178, 204, 229)
tealB      = array.from( 25,  51,  76, 102, 128, 153, 178, 153, 229)
blueG      = array.from( 25,  51,  76, 102, 128, 153, 178, 204, 229)
purpleG    = array.from( 25,  51,  76, 102, 127, 153, 178, 204, 229)
grayR      = array.from(  0,  32,  64,  96, 128, 160, 192, 224, 225)

redRGB     =    redR.groupRGB(   redG, redG),    strRed     = stringRGB(    redRGB), colorRed     =     redRGB.createColor()
orangeRGB  =    redR.groupRGB(orangeG, redG),    strOrange  = stringRGB( orangeRGB), colorOrange  =  orangeRGB.createColor()
yellowRGB  =    redR.groupRGB(   redR, redG),    strYellow  = stringRGB( yellowRGB), colorYellow  =  yellowRGB.createColor()
limeRGB    = orangeG.groupRGB(   redR, redG),    strLime    = stringRGB(   limeRGB), colorLime    =    limeRGB.createColor()
greenRGB   =    redG.groupRGB(   redR, redG),    strGreen   = stringRGB(  greenRGB), colorGreen   =   greenRGB.createColor()
tealRGB    =    redG.groupRGB(   redR, orangeG), strTeal    = stringRGB(   tealRGB), colorTeal    =    tealRGB.createColor()
aquaRGB    =    redG.groupRGB(   redR, redR),    strAqua    = stringRGB(   aquaRGB), colorAqua    =    aquaRGB.createColor()
blueRGB    =    redG.groupRGB(  blueG, redR),    strBlue    = stringRGB(   blueRGB), colorBlue    =    blueRGB.createColor()
navyRGB    =    redG.groupRGB(   redG, redR),    strNavy    = stringRGB(   navyRGB), colorNavy    =    navyRGB.createColor()
purpleRGB  = purpleG.groupRGB(   redG, redR),    strPurple  = stringRGB( purpleRGB), colorPurple  =  purpleRGB.createColor()
fuchsiaRGB =    redR.groupRGB(   redG, purpleG), strFuchsia = stringRGB(fuchsiaRGB), colorFuchsia = fuchsiaRGB.createColor()
maroonRGB  =    redR.groupRGB(   redG, redR),    strMaroon  = stringRGB( maroonRGB), colorMaroon  =  maroonRGB.createColor()
grayRGB    =   grayR.groupRGB(  grayR, grayR),   strGray    = stringRGB(   grayRGB), colorGray    =    grayRGB.createColor()

palette.set( 0, properties.new(titleRed,         'Red', color.red))
palette.set( 1, properties.new(titleOrange,   'Orange', color.orange))
palette.set( 2, properties.new(titleYellow,   'Yellow', color.yellow))
palette.set( 3, properties.new(titleLime,       'Lime', color.lime))
palette.set( 4, properties.new(titleGreen,     'Green', color.green))
palette.set( 5, properties.new(titleTeal,       'Teal', color.teal))
palette.set( 6, properties.new(titleAqua,       'Aqua', color.aqua))
palette.set( 7, properties.new(titleBlue,       'Blue', color.blue))
palette.set( 8, properties.new(titleNavy,       'Navy', color.navy))
palette.set( 9, properties.new(titlePurple,   'Purple', color.purple))
palette.set(10, properties.new(titleFuchsia, 'Fuchsia', color.fuchsia))
palette.set(11, properties.new(titleMaroon,   'Maroon', color.maroon))
palette.set(12, properties.new(titleGray,       'Gray', color.gray))

[i0, i1] = switch showGradient 
    true  => [intGradient, intGradient]
    false => [          0,           8]
//#endregion

//#region ———————————————————— 4. Constructs
if barstate.islast
    for i = i0 to i1
        palette.boolCell(tbl, 0, i, strRed,     colorRed,     colorText)
        palette.boolCell(tbl, 1, i, strOrange,  colorOrange,  colorText)
        palette.boolCell(tbl, 2, i, strYellow,  colorYellow,  colorText)
        palette.boolCell(tbl, 3, i, strLime,    colorLime,    colorText)
        palette.boolCell(tbl, 4, i, strGreen,   colorGreen,   colorText)
        palette.boolCell(tbl, 5, i, strTeal,    colorTeal,    colorText)
        palette.boolCell(tbl, 6, i, strAqua,    colorAqua,    colorText)
        palette.boolCell(tbl, 7, i, strBlue,    colorBlue,    colorText)
        palette.boolCell(tbl, 8, i, strNavy,    colorNavy,    colorText)
        palette.boolCell(tbl, 9, i, strPurple,  colorPurple,  colorText)
        palette.boolCell(tbl,10, i, strMaroon,  colorMaroon,  colorText)
        palette.boolCell(tbl,11, i, strFuchsia, colorFuchsia, colorText)
        palette.boolCell(tbl,12, i, strGray,    colorGray,    colorText)
    for i = i0 to i1
        palette.setCellText(tbl, 0, i, strRed,     redRGB)
        palette.setCellText(tbl, 1, i, strOrange,  orangeRGB)
        palette.setCellText(tbl, 2, i, strYellow,  yellowRGB)
        palette.setCellText(tbl, 3, i, strLime,    limeRGB)
        palette.setCellText(tbl, 4, i, strGreen,   greenRGB)
        palette.setCellText(tbl, 5, i, strTeal,    tealRGB)
        palette.setCellText(tbl, 6, i, strAqua,    aquaRGB)
        palette.setCellText(tbl, 7, i, strBlue,    blueRGB)
        palette.setCellText(tbl, 8, i, strNavy,    navyRGB)
        palette.setCellText(tbl, 9, i, strPurple,  purpleRGB)
        palette.setCellText(tbl,10, i, strMaroon,  maroonRGB)
        palette.setCellText(tbl,11, i, strFuchsia, fuchsiaRGB)
        palette.setCellText(tbl,12, i, strGray,    grayRGB)
    for i = 0 to 12
        if showTitle and palette.get(i).enable
            tbl.cell(i, 0, palette.get(i).title, bgcolor = palette.get(i).col, text_size = fontSize, text_color = color.white)
//#endregion