Better Contrast (NTSC Optimized)

This lightweight utility library automatically selects the optimal text color (black or white) for any given background color, ensuring maximum readability for labels, tables, and UI elements.
Unlike standard libraries that use the HSP model or simple averaging, this library utilizes the NTSC Perceived Brightness formula.
🟢 Why NTSC?
The human eye is significantly more sensitive to green light than red or blue. Standard formulas often miscalculate brightness for high‑energy colors like yellow (red + green) or cyan, resulting in white text on bright yellow backgrounds — which is hard to read.
The NTSC formula weights colors based on human perception:
Brightness = (Red * 0.299) + (Green * 0.587) + (Blue * 0.114)
By heavily weighting the green channel (58.7%), this method correctly identifies yellow and cyan as “bright” backgrounds, forcing the text to black for superior contrast.
🛠 Usage
Import the library:
import Robertsanktov/Better_Contrast/1 as contrast
Use the method directly on any color variable:
textColor = myBackgroundColor.contrast()
Parameters
- threshold: (optional) brightness cutoff (0.0–1.0 or 0–255). Default is 0.55.
Higher values force more white text; lower values force more black text.
Pine Bibliothek
Ganz im Sinne von TradingView hat dieser Autor seinen/ihren Pine Code als Open-Source-Bibliothek veröffentlicht. Auf diese Weise können nun auch andere Pine-Programmierer aus unserer Community den Code verwenden. Vielen Dank an den Autor! Sie können diese Bibliothek privat oder in anderen Open-Source-Veröffentlichungen verwenden. Die Nutzung dieses Codes in einer Veröffentlichung wird in unseren Hausregeln reguliert.
Haftungsausschluss
Pine Bibliothek
Ganz im Sinne von TradingView hat dieser Autor seinen/ihren Pine Code als Open-Source-Bibliothek veröffentlicht. Auf diese Weise können nun auch andere Pine-Programmierer aus unserer Community den Code verwenden. Vielen Dank an den Autor! Sie können diese Bibliothek privat oder in anderen Open-Source-Veröffentlichungen verwenden. Die Nutzung dieses Codes in einer Veröffentlichung wird in unseren Hausregeln reguliert.