Skip to content

Instantly share code, notes, and snippets.

@PowerX-NOT
Last active October 1, 2024 16:27
Show Gist options
  • Save PowerX-NOT/0f23e7133768f04ccc15d5dc3d2aa2c1 to your computer and use it in GitHub Desktop.
Save PowerX-NOT/0f23e7133768f04ccc15d5dc3d2aa2c1 to your computer and use it in GitHub Desktop.
import subprocess
import sys
import math
# Function to install colorama if not present
def install_colorama():
try:
import colorama
except ImportError:
print("colorama is not installed. Installing now...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "colorama"])
# Call the function to ensure colorama is installed
install_colorama()
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
def calculate_backlight_ratio(max_backlight, nits_with_hbm, nits_without_hbm):
ratio = (max_backlight * nits_with_hbm) / nits_without_hbm
return ratio
# Error handling for LUT type input
while True:
try:
print("")
lut_type = input("Enter the LUT type ('FD' for FrameworkDimming, 'KD' for KernelDimming): ").strip().upper()
if lut_type not in ['FD', 'KD']:
raise ValueError(Fore.RED + "Invalid LUT type.")
break
except ValueError as e:
print(Fore.RED + str(e))
print(Fore.YELLOW + "Invalid LUT type. Please choose 'FD' for FrameworkDimming or 'KD' for KernelDimming.")
# Error handling for max_backlight input
while True:
try:
print("")
max_backlight = float(input(Fore.CYAN + "Enter the maximal backlight value (1023/2047/4095): "))
if max_backlight not in [1023, 2047, 4095]:
raise ValueError(Fore.RED + "Invalid max_backlight value.")
break
except ValueError as e:
print(Fore.RED + str(e))
print(Fore.YELLOW + "Please enter a valid maximal backlight value: 1023, 2047, or 4095.")
nits_with_hbm = float(input(Fore.CYAN + "Enter the nits with HBM: "))
nits_without_hbm = float(input(Fore.CYAN + "Enter the nits without HBM: "))
gamma_factor = float(input(Fore.CYAN + "Enter the Gamma factor: "))
# Choose A2 values based on max_backlight
if max_backlight == 2047:
A2_values = [0, 3, 13, 31, 58, 96, 143, 200, 269, 348, 439, 551, 667, 794, 934, 1086, 1250, 1427, 1618, 1821, 2047]
dc_threshold = 610
dc_A2_values = [0, 1, 4, 9, 18, 29, 43, 60, 81, 106, 132, 163, 198, 237, 277, 323, 373, 428, 481, 544, 610]
elif max_backlight == 4095:
A2_values = [0, 6, 26, 62, 116, 192, 286, 400, 538, 696, 878, 1102, 1334, 1588, 1868, 2172, 2500, 2854, 3236, 3642, 4095]
dc_threshold = 1220
dc_A2_values = [0, 2, 8, 18, 36, 58, 86, 120, 162, 212, 264, 326, 396, 474, 554, 646, 746, 856, 962, 1088, 1220]
elif max_backlight == 1023:
A2_values = [0, 1, 16, 33, 48, 97, 146, 194, 243, 292, 341, 389, 438, 487, 535, 584, 633, 682, 730, 779, 828, 876, 925, 974, 1023]
dc_threshold = 320
dc_A2_values = [0, 1, 3, 5, 9, 15, 22, 32, 43, 56, 69, 86, 104, 124, 145, 169, 196, 224, 253, 285, 320]
# Calculate backlight ratio
backlight_ratio = calculate_backlight_ratio(max_backlight, nits_with_hbm, nits_without_hbm)
print("")
print(Fore.GREEN + f"The backlight ratio is: {backlight_ratio}")
print("")
if lut_type == 'KD':
# Print results for qcom,disp-fod-dim-lut
print(Fore.MAGENTA + "qcom,disp-fod-dim-lut = <")
print(Fore.MAGENTA + "\t/* brightness, alpha */")
for A2 in A2_values:
result = (1 - math.pow(A2 / backlight_ratio, 1 / gamma_factor)) * 255
hex_result = hex(int(result)).upper()
print(f"{Fore.MAGENTA}\t{A2}\t{hex_result}")
print(Fore.MAGENTA + ">;")
print("")
# Print results for qcom,disp-dc-dim-lut
print(Fore.MAGENTA + "qcom,disp-dc-dim-lut = <")
print(Fore.MAGENTA + "\t/* brightness, alpha */")
for A2 in dc_A2_values:
result = (1 - math.pow(A2 / dc_threshold, 1 / gamma_factor)) * 255
hex_result = hex(int(result)).upper()
print(f"{Fore.MAGENTA}\t{A2}\t{hex_result}")
print(Fore.MAGENTA + ">;")
print("")
elif lut_type == 'FD':
# Print for framework dimming
print("Array of brightness-alpha LUT for framework dimming:")
print("")
for A2 in A2_values:
result = (1 - math.pow(A2 / backlight_ratio, 1 / gamma_factor)) * 255
int_result = int(result)
print(Fore.MAGENTA + f"<item>{A2},{int_result}</item>")
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment