PINE LIBRARY

Better Contrast (NTSC Optimized)

70
Library 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.

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.