PINE LIBRARY
Better Contrast (NTSC Optimized)

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.
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 kitaplığı
Gerçek TradingView ruhuyla, yazar bu Pine kodunu açık kaynaklı bir kütüphane olarak yayınladı, böylece topluluğumuzdaki diğer Pine programcıları onu yeniden kullanabilir. Yazarı tebrik ederiz! Bu kütüphaneyi özel olarak veya diğer açık kaynaklı yayınlarda kullanabilirsiniz, ancak bu kodun yayınlarda yeniden kullanılması Topluluk Kuralları tarafından yönetilir.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.
Pine kitaplığı
Gerçek TradingView ruhuyla, yazar bu Pine kodunu açık kaynaklı bir kütüphane olarak yayınladı, böylece topluluğumuzdaki diğer Pine programcıları onu yeniden kullanabilir. Yazarı tebrik ederiz! Bu kütüphaneyi özel olarak veya diğer açık kaynaklı yayınlarda kullanabilirsiniz, ancak bu kodun yayınlarda yeniden kullanılması Topluluk Kuralları tarafından yönetilir.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.