TradingView
upslidedown
2022年7月12日午後3時15分

Light or Dark Mode Tutorial - Luminance Detection 

SPDR S&P 500 ETF TRUSTArca

詳細

As a colorblind trader, I think accessibility is a big deal. This script auto detects the chart background color and optimizes text color based on luminance.

Luminance detection is based on pine script new chart.bg_color feature, allowing lines, tables, etc to be optimized. Thanks to TV team for releasing this in the latest update/blog post today! This makes it simple to optimize scripts based off the knowledge that max luminance = 765 (rgb 255 + 255 + 255), thus we know that lum <= 383 is "dark mode" and lum > 383 is "light mode".

Try changing the chart background color and see how this script changes the table printed on the chart. I hope more script authors will begin to utilize this concept and that even better contrast detection may be built future pine script iterations.
コメント
wildhood
This is awesome! Thank you for this script! I'm applying it to my core indicators, that live on my screen. (I frequently switch to a dark background chart for late nights, but during the day I prefer a white background. But having to manually toggle a "dark mode" switch that I used on my indicators was annoying!) Any way. Thank you!
upslidedown
@wildhood, glad this was helpful. One other trick is that TV has a chart.fg_color variable that you can use to essentially replicate whatever is used on the price scale on the right side of charts. fg_color will be the opposite of whatever contrast mode you have TradingView running on. It's a nice trick (that sometimes prevents you from having to do any sort of detection at all) and I should probably add that into this code just so people see that in example too!
BarefootJoey
Beautiful utility. Thank you for this!
mikeshow-c
Thank you very much, that is very helpful.
詳細