Although you cannot detect the specific background color, you can however know that either white or black text is the best color for dark and light backgrounds of any color respectively.
So implementing dark mode may do the trick!
Here's an example in a script I wrote recently:
bool dark_mode = input.bool(true, 'Dark Mode', group='Swag')
color neutral_col = dark_mode ? color.white : color.black
color back_col = dark_mode ? color.black : color.white
color max_col = index_up ? color.aqua : neutral_col
color index_col = index_up ? color.lime : index_down ? color.yellow : neutral_col
color min_col = index_down ? color.purple : neutral_col
if barstate.islast
table.cell(stats, 0, 4, 'How Terra', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
table.cell(stats, 1, 4, 'Very Luna', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
