VB与Java颜色值的转换

正常的 RGB 颜色的有效范围,是从 0 到 16,777,215 (&HFFFFFF&)。每种颜色的设置值(属性或参数)都是一个四字节的整数。对于这个
范围内的数,其高字节都是 0,而低三个字节,从最低字节到第三个字节,分别定义了红、绿、蓝三种颜色的值。红、绿、蓝三种成分都
是用 0 到 255 (&HFF) 之间的数表示。因此,可以用十六进制数按照下述语法来指定颜色:

&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被用来处理
各个操作系统不同的系统颜色。

    public final static SystemColor desktop = new SystemColor((byte)DESKTOP);

    public final static SystemColor activeCaption = new SystemColor((byte)ACTIVE_CAPTION);

    public final static SystemColor activeCaptionText = new SystemColor((byte)ACTIVE_CAPTION_TEXT);

	...
	
	private static int[] systemColors;

    static {
      updateSystemColors();
    }

    /**
     * Called from <init> & toolkit to update the above systemColors cache.
     */
    private static void updateSystemColors() {
        if (!GraphicsEnvironment.isHeadless()) {
            Toolkit.getDefaultToolkit().loadSystemColors(systemColors);
        }
    }

	private SystemColor(byte index) {
        super(0, 0, 0);
		value = index;
    }

    public int getRGB() {
	return systemColors[value];
    }

SystemColor通过静态域初始化系统颜色的数组,我们可以利用它的初始化结果作为当前操作系统的系统颜色集合,来完成VB颜色到Java颜色的转换。

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 color
		System.out.println(tester.convertVB2JavaColor(0x80000007) == SystemColor.menuText.getRGB());
		
		// 2.Test for System color
		System.out.println(tester.convertVB2JavaColor(0x004207) == (0xFF | 0x4207));
		
		// 3.Test for invalid input argument
		try {
			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 vbColorHex	0x80000000 - 0x80000018 for VB system color, 
	 * 							0x00AB1234 for palette color
	 * 
	 * @return					Bits 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 1
			javaColorHex = VB2JavaSystemColorMapping.get(vbColorHex);
		}
		else {			
			throw new IllegalArgumentException("Illegal hex color argument: " + vbColorHex);
		}
		
		return javaColorHex;
	}
	
}

简单跑了下,不知道是否正确,有没有人有写这方面代码的经验?

原文地址:https://www.cnblogs.com/xiaomaohai/p/6157800.html