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.
ไลบรารีไพน์
ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนได้เผยแพร่ Pine code นี้เป็นโอเพนซอร์สไลบรารีเพื่อให้ Pine โปรแกรมเมอร์คนอื่นในชุมชนของเราสามารถนำไปใช้ซ้ำได้ ต้องขอบคุณผู้เขียน! คุณสามารถใช้ไลบรารีนี้ในแบบส่วนตัวหรือในการเผยแพร่แบบโอเพนซอร์สอื่น ๆ แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน
ไลบรารีไพน์
ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนได้เผยแพร่ Pine code นี้เป็นโอเพนซอร์สไลบรารีเพื่อให้ Pine โปรแกรมเมอร์คนอื่นในชุมชนของเราสามารถนำไปใช้ซ้ำได้ ต้องขอบคุณผู้เขียน! คุณสามารถใช้ไลบรารีนี้ในแบบส่วนตัวหรือในการเผยแพร่แบบโอเพนซอร์สอื่น ๆ แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน