3

Would like to detect if user has dark mode on or if it has black background set in trading view chart. My script uses black lines because it's adjusted to white background chart, and when someone uses dark background, they don't see a lot of script elements.

Is there a way to detect chart background color or if user is using "dark mode" option in TradingView?

Thank you.

Mikeyy
  • 285
  • 1
  • 7

2 Answers2

3

No. But plot() function has a parameter called editable. If you set it to true, then the user can change the plot style in Format dialog. Default is true. Just make the title of the plot meaningful so user can understand which plot they are modifying.

//@version=3
study("My Script", overlay=false)
plot(series=close, title="My close", color=black, editable=true)

enter image description here

vitruvius
  • 15,740
  • 3
  • 16
  • 26
1

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)

dark mode in action indicator settings

DogeCode
  • 346
  • 2
  • 10
  • 1
    This is a good idea. You reminded me to check if TV added white/dark theme detection in pine v5, but no luck for now. – Mikeyy Oct 03 '22 at 10:39