Random Color Generator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed
// © RicardoSantos

//@version=5
indicator('Random Color Generator', overlay=true)
i_preset = input.string('Light+Dark', title='Background', options=['Light', 'Dark', 'Light+Dark', 'Custom'])
i_h_min = input.int(0, minval=0, maxval=127, title='H', group='HSLT', inline='H')
i_h_max = input.int(255, minval=128, maxval=255, title='', group='HSLT', inline='H')
i_s_min = input.int(0, minval=0, maxval=50, title='S', group='HSLT', inline='S')
i_s_max = input.int(100, minval=51, maxval=100, title='', group='HSLT', inline='S')
i_l_min = input.int(30, minval=0, maxval=51, title='L', group='HSLT', inline='L')
i_l_max = input.int(70, minval=51, maxval=100, title='', group='HSLT', inline='L')
i_transparency = input.int(0, minval=0, maxval=100, title='T', group='HSLT', inline='T')

// ************** This part of the code is taken from // © RicardoSantos
// https://www.tradingview.com/script/4jdGt6Xo-Function-HSL-color/
f_color_hsl(_hue, _saturation, _lightness, _transparency) =>  //{
//  @function: returns HSL color.
//  @reference: https://stackoverflow.com/questions/36721830/convert-hsl-to-rgb-and-hex
    float _l1 = _lightness / 100.0
    float _a = _saturation * math.min(_l1, 1.0 - _l1) / 100.0
    float _rk = (0.0 + _hue / 30.0) % 12.0
    float _r = 255.0 * (_l1 - _a * math.max(math.min(_rk - 3.0, 9.0 - _rk, 1.0), -1.0))
    float _gk = (8.0 + _hue / 30.0) % 12.0
    float _g = 255.0 * (_l1 - _a * math.max(math.min(_gk - 3.0, 9.0 - _gk, 1.0), -1.0))
    float _bk = (4.0 + _hue / 30.0) % 12.0
    float _b = 255.0 * (_l1 - _a * math.max(math.min(_bk - 3.0, 9.0 - _bk, 1.0), -1.0))
    color.rgb(_r, _g, _b, _transparency)
//}
// ************** End of borrowed code

avoidLight = i_preset == 'Light' or i_preset == 'Light+Dark'
avoidDark = i_preset == 'Dark' or i_preset == 'Light+Dark'
custom = i_preset == 'Custom'
l_min = custom ? i_l_min : avoidDark ? 30 : 0
l_max = custom ? i_l_max : avoidLight ? 70 : 100

h_min = custom ? i_h_min : 0
h_max = custom ? i_h_max : 255

s_min = custom ? i_s_min : 0
s_max = custom ? i_s_max : 100


f_random_color(h_min, h_max, s_min, s_max, l_min, l_max, transparency) =>
    f_color_hsl(math.random(h_min, h_max), math.random(s_min, s_max), math.random(l_min, l_max), transparency)

barcolor(f_random_color(h_min, h_max, s_min, s_max, l_min, l_max, i_transparency))

