Simplify color temp display names and VCP hex parsing

Refactored color temperature display name formatting to remove
hexadecimal values from user-facing strings, returning only the
preset or custom name. Updated fallback and custom value handling
to be more concise. Replaced the TryParseHexCode helper with
direct string slicing and parsing for VCP codes. Updated
documentation comments to match new formatting. These changes
improve code clarity and provide a cleaner UI.
This commit is contained in:
Yu Leng
2025-12-12 10:59:18 +08:00
parent 4557c509e5
commit f47abb43e9
4 changed files with 24 additions and 34 deletions

View File

@@ -48,26 +48,18 @@ namespace PowerDisplay.Common.Utils
/// </summary>
/// <param name="vcpValue">The VCP value.</param>
/// <param name="customName">Optional custom name from capabilities string.</param>
/// <returns>Formatted display name with hex value.</returns>
/// <returns>Formatted display name.</returns>
public static string FormatColorTemperatureDisplayName(int vcpValue, string? customName = null)
{
var hexValue = $"0x{vcpValue:X2}";
// Priority: use name from VCP capabilities if available
if (!string.IsNullOrEmpty(customName))
{
return $"{customName} ({hexValue})";
return customName;
}
// Fall back to standard VCP value name from shared library
var standardName = VcpNames.GetValueName(NativeConstants.VcpCodeSelectColorPreset, vcpValue);
if (standardName != null)
{
return $"{standardName} ({hexValue})";
}
// Unknown value
return $"Manufacturer Defined ({hexValue})";
return VcpNames.GetValueName(NativeConstants.VcpCodeSelectColorPreset, vcpValue)
?? "Manufacturer Defined";
}
/// <summary>
@@ -80,8 +72,8 @@ namespace PowerDisplay.Common.Utils
{
var standardName = VcpNames.GetValueName(NativeConstants.VcpCodeSelectColorPreset, vcpValue);
return string.IsNullOrEmpty(standardName)
? $"Custom (0x{vcpValue:X2})"
: $"{standardName} (0x{vcpValue:X2}) - Custom";
? "Custom"
: $"{standardName} (Custom)";
}
}
}

View File

@@ -93,7 +93,7 @@ namespace PowerDisplay.Common.Utils
var cleanCode = code.Trim();
if (cleanCode.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
cleanCode = cleanCode.Substring(2);
cleanCode = cleanCode[2..];
}
if (int.TryParse(cleanCode, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int codeInt))

View File

@@ -11,10 +11,10 @@ namespace PowerDisplay.Common.Utils
public static class MonitorValueConverter
{
/// <summary>
/// Formats a VCP color temperature value as a display name with preset name.
/// Formats a VCP color temperature value as a display name.
/// </summary>
/// <param name="vcpValue">The VCP preset value (e.g., 0x05).</param>
/// <returns>Formatted string like "6500K (0x05)" or "sRGB (0x01)".</returns>
/// <returns>Display name like "6500K" or "sRGB".</returns>
public static string FormatColorTemperatureDisplay(int vcpValue)
{
return ColorTemperatureHelper.FormatColorTemperatureDisplayName(vcpValue);

View File

@@ -129,21 +129,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
OnPropertyChanged(nameof(ColorPresetsForDisplay));
}
/// <summary>
/// Parses a hexadecimal string (with or without "0x" prefix) to an integer.
/// </summary>
private static bool TryParseHexCode(string hexString, out int result)
{
result = 0;
if (string.IsNullOrEmpty(hexString))
{
return false;
}
var cleanHex = hexString.Replace("0x", string.Empty);
return int.TryParse(cleanHex, System.Globalization.NumberStyles.HexNumber, null, out result);
}
public MonitorInfo()
{
}
@@ -643,7 +628,13 @@ namespace Microsoft.PowerToys.Settings.UI.Library
// Find VCP code 0x14 (Color Temperature / Select Color Preset)
var colorTempVcp = _vcpCodesFormatted.FirstOrDefault(v =>
TryParseHexCode(v.Code, out int code) && code == NativeConstants.VcpCodeSelectColorPreset);
!string.IsNullOrEmpty(v.Code) &&
int.TryParse(
v.Code.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? v.Code[2..] : v.Code,
System.Globalization.NumberStyles.HexNumber,
System.Globalization.CultureInfo.InvariantCulture,
out int code) &&
code == NativeConstants.VcpCodeSelectColorPreset);
// No VCP 0x14 or no values
if (colorTempVcp == null || colorTempVcp.ValueList == null || colorTempVcp.ValueList.Count == 0)
@@ -655,7 +646,14 @@ namespace Microsoft.PowerToys.Settings.UI.Library
var colorTempValues = colorTempVcp.ValueList
.Select(valueInfo =>
{
bool parsed = TryParseHexCode(valueInfo.Value, out int vcpValue);
var hex = valueInfo.Value;
if (string.IsNullOrEmpty(hex))
{
return (VcpValue: 0, Name: valueInfo.Name);
}
var cleanHex = hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase) ? hex[2..] : hex;
bool parsed = int.TryParse(cleanHex, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out int vcpValue);
return (VcpValue: parsed ? vcpValue : 0, Name: valueInfo.Name);
})
.Where(x => x.VcpValue > 0);