如何将 hexString 转换为 SWT Color?

How to convert hexString to SWT Color?

当您有 hexString #FFF080 并且您想将其转换为 org.eclipse.swt.graphics.Color 时,最好的方法是什么?

当然可以!还有许多其他方法可以解决该问题。 以下是解决方案之一。

public static Color decode(Display display, String hexString) {
            try {
                Integer intval = Integer.decode(hexString);
                int i = intval.intValue();
                return new Color(display, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
            } catch (NumberFormatException nfe) {
                return null;
            }
        }

您可以在java.awt.Color.decode(str);

找到上述实现源代码

或者简单地这样做:

public static Color decode(Display display, String hexString)
{
    try
    {
        java.awt.Color c = java.awt.Color.decode(hexString);

        return new Color(display, c.getRed(), c.getGreen(), c.getBlue());
    }
    catch(NumberFormatException e)
    {
        return null;
    }
}