VB与Java颜色值的转换
&HBBGGRR&
BB 指定蓝颜色的值,GG 指定绿颜色的值,RR 指定红颜色的值。每个数段都是两位十六进制数,即从 00 到 FF。&H808080&将最高位设置为 1,就改变了颜色值的含义:颜色值不再代表一种 RGB 颜色,而是一种从 Windows"控制面板"指定的环境范围颜色。这些数值对应的系统颜色范围是从 &H80000000 到 &H80000015。 以下就是这些特殊的系统颜色值及其含义:
Constant
Value
Description
vbScrollBars
0x80000000
Scroll bar color
vbDesktop
0x80000001
Desktop color
vbActiveTitleBar
0x80000002
Color of the title bar for the active window
vbInactiveTitleBar
0x80000003
Color of the title bar for the inactive window
vbMenuBar
0x80000004
Menu background color
vbWindowBackground
0x80000005
Window background color
vbWindowFrame
0x80000006
Window frame color
vbMenuText
0x80000007
Color of text on menus
vbWindowText
0x80000008
Color of text in windows
vbTitleBarText
0x80000009
Color of text in caption, size box, and scroll arrow
vbActiveBorder
0x8000000A
Border color of active window
vbInactiveBorder
0x8000000B
Border color of inactive window
vbApplicationWorkspace
0x8000000C
Background color of multiple-document interface (MDI) applications
vbHighlight
0x8000000D
Background color of items selected in a control
vbHighlightText
0x8000000E
Text color of items selected in a control
vbButtonFace
0x8000000F
Color of shading on the face of command buttons
vbButtonShadow
0x80000010
Color of shading on the edge of command buttons
vbGrayText
0x80000011
Grayed (disabled) text
vbButtonText
0x80000012
Text color on push buttons
vbInactiveCaptionText
0x80000013
Color of text in an inactive caption
vb3DHighlight
0x80000014
Highlight color for 3-D display elements
vb3DDKShadow
0x80000015
Darkest shadow color for 3-D display elements
vb3DLight
0x80000016
Second lightest 3-D color after vb3DHighlight
vbInfoText
0x80000017
Color of text in ToolTips
vbInfoBackground
0x80000018
Background color of ToolTips
首先,Java中的颜色的整数值是这样组成的:0~7位是蓝色值, 8~15位是绿色值, 16~23位是红色值, 24~31位是Alpha值。一个标准的RGB值转成Java的需要设置最高8位为FF(默认的Alpha值)。此外,对于像VB中这些特殊的系统颜色来说,Java中的SystemColor被用来处理各个操作系统不同的系统颜色。
package com.cdai.jd;import java.awt.SystemColor;import java.util.HashMap;public class SystemColorTest {public static void main(String[] args) {SystemColorTest tester = new SystemColorTest();// 1.Test for Palette colorSystem.out.println(tester.convertVB2JavaColor(0x80000007) == SystemColor.menuText.getRGB());// 2.Test for System colorSystem.out.println(tester.convertVB2JavaColor(0x004207) == (0xFF | 0x4207));// 3.Test for invalid input argumenttry {System.out.println(tester.convertVB2JavaColor(0x8100000A));} catch (Exception e) {System.out.println("Expect exception here.");}}private static HashMap<Integer, Integer> VB2JavaSystemColorMapping = new HashMap<Integer, Integer>();/** * Color constants refer to: * http://msdn.microsoft.com/en-us/library/office/gg264801.aspx */static {VB2JavaSystemColorMapping.put(0x80000000, SystemColor.scrollbar.getRGB());VB2JavaSystemColorMapping.put(0x80000001, SystemColor.desktop.getRGB());VB2JavaSystemColorMapping.put(0x80000002, SystemColor.activeCaption.getRGB());VB2JavaSystemColorMapping.put(0x80000003, SystemColor.inactiveCaption.getRGB());VB2JavaSystemColorMapping.put(0x80000004, SystemColor.menu.getRGB());VB2JavaSystemColorMapping.put(0x80000005, SystemColor.window.getRGB());VB2JavaSystemColorMapping.put(0x80000006, SystemColor.scrollbar.getRGB());//Window frame color?VB2JavaSystemColorMapping.put(0x80000007, SystemColor.menuText.getRGB());VB2JavaSystemColorMapping.put(0x80000008, SystemColor.windowText.getRGB());VB2JavaSystemColorMapping.put(0x80000009, SystemColor.activeCaptionText.getRGB());VB2JavaSystemColorMapping.put(0x8000000A, SystemColor.activeCaptionBorder.getRGB());VB2JavaSystemColorMapping.put(0x8000000B, SystemColor.inactiveCaptionBorder.getRGB());VB2JavaSystemColorMapping.put(0x8000000C, SystemColor.scrollbar.getRGB());//Background color of multiple-document interface (MDI) applications?VB2JavaSystemColorMapping.put(0x8000000D, SystemColor.textHighlight.getRGB());VB2JavaSystemColorMapping.put(0x8000000E, SystemColor.textHighlightText.getRGB());VB2JavaSystemColorMapping.put(0x8000000F, SystemColor.scrollbar.getRGB());//Color of shading on the face of command buttons?VB2JavaSystemColorMapping.put(0x80000010, SystemColor.scrollbar.getRGB());//Color of shading on the edge of command buttons?VB2JavaSystemColorMapping.put(0x80000011, SystemColor.textInactiveText.getRGB());VB2JavaSystemColorMapping.put(0x80000012, SystemColor.controlText.getRGB());VB2JavaSystemColorMapping.put(0x80000013, SystemColor.inactiveCaptionText.getRGB());VB2JavaSystemColorMapping.put(0x80000014, SystemColor.controlHighlight.getRGB());VB2JavaSystemColorMapping.put(0x80000015, SystemColor.controlDkShadow.getRGB());VB2JavaSystemColorMapping.put(0x80000016, SystemColor.controlLtHighlight.getRGB());VB2JavaSystemColorMapping.put(0x80000017, SystemColor.infoText.getRGB());VB2JavaSystemColorMapping.put(0x80000018, SystemColor.info.getRGB());}/** * Convert color hex value in VB to Java color hex. * * @param vbColorHex0x80000000 - 0x80000018 for VB system color, * 0x00AB1234 for palette color * * @returnBits 24-31 are alpha (FF as default), * 16-23 are red, * 8-15 are green, * 0-7 are blue */public int convertVB2JavaColor(int vbColorHex) {int javaColorHex;int highByte = (vbColorHex >>> 24);if (highByte == 0) {// Palette color if high byte is 0.javaColorHex = 0xFF | vbColorHex;}else if (highByte == 128) {// System color if highest bit is 1javaColorHex = VB2JavaSystemColorMapping.get(vbColorHex);}else {throw new IllegalArgumentException("Illegal hex color argument: " + vbColorHex);}return javaColorHex;}}简单跑了下,不知道是否正确,有没有人有写这方面代码的经验?