@pcinfogmach כתב בכיצד להשוות ערך range.Font.Color לצבע הקסדצימלי ב-VSTO?:
בכתבה מוצג קוד לחישוב דינאמי של הערכים השונים של גווני ערכת הנושא (בערכים דצימליים המומרים ל-hex מיוחד לוורד).
מצו"ב הקוד מתורגם ל-C# בדגש דווקא על בהירות והבנה (במחיר זניח של יעילות).
using System;
using Microsoft.Office.Interop.Word;
public class ThemeColorsHelperBase
{
// Color code components
private readonly string _hexPrefix = "0x"; // Prefix used for hexadecimal strings
private readonly string _themeColorFlag = "D"; // Identifier for theme color
private readonly string _zeroByte = "00"; // Unused byte in the color code
private readonly string _maxValue = "FF"; // Used when no tint/shade is applied
/// <summary>
/// Generates an int representation of a theme color with its tint or shade.
/// </summary>
/// <param name="themeColorIndex">Theme color index (e.g., Accent1, Text1)</param>
/// <param name="tintAndShade">accepts percantage double (0.50, 0.25 etc.); positive (tint/lighten) or negative (shade/darken).</param>
/// <returns></returns>
public long GetThemeColor(WdThemeColorIndex themeColorIndex, double brightness)
{
string hex = GenerateThemeColorHex(themeColorIndex, brightness);
return Convert.ToInt64(hex, 16);
}
private string GenerateThemeColorHex(WdThemeColorIndex themeColorIndex, double brightness)
{
string themeColorHex = ((int)themeColorIndex).ToString("X");
string tintHex = brightness >= 0 ? ((int)((1 - brightness) * 0xFF)).ToString("X2") : _maxValue;
string shadeHex = brightness < 0 ? ((int)((1 + brightness) * 0xFF)).ToString("X2") : _maxValue;
string fullHex = _hexPrefix + _themeColorFlag + themeColorHex + _zeroByte + tintHex + shadeHex;
return fullHex;
}
}