16進表記のカラー文字列←→RGB int 変換

「16進表記のカラー文字列←→RGB int 変換」の編集履歴(バックアップ)一覧はこちら

16進表記のカラー文字列←→RGB int 変換」(2010/12/06 (月) 11:21:22) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

public class HexRGBColorSample { public static void main(String[] args) { { // 白 int[] rgb = toRGB("FFFFFF"); System.out.println("255? " + rgb[0]); System.out.println("255? " + rgb[1]); System.out.println("255? " + rgb[2]); System.out.println("ffffff? " + toHexString(rgb)); } { // 黒 int[] rgb = toRGB("000000"); System.out.println("0? " + rgb[0]); System.out.println("0? " + rgb[1]); System.out.println("0? " + rgb[2]); System.out.println("000000? " + toHexString(rgb)); } { // オレンジ int[] rgb = toRGB("ffa500"); System.out.println("255? " + rgb[0]); System.out.println("165? " + rgb[1]); System.out.println("0? " + rgb[2]); System.out.println("ffa500? " + toHexString(rgb)); } } public static int[] toRGB(String hexColorRGB) { char rgb[] = hexColorRGB.toCharArray(); int[] rgbInt = new int[3]; rgbInt[0] = Character.digit(rgb[0], 16) * 16 + Character.digit(rgb[1], 16); rgbInt[1] = Character.digit(rgb[2], 16) * 16 + Character.digit(rgb[3], 16); rgbInt[2] = Character.digit(rgb[4], 16) * 16 + Character.digit(rgb[5], 16); return rgbInt; } public static String toHexString(int r, int g, int b) { StringBuffer buf = new StringBuffer(); if (r < 16) { buf.append("0").append(Integer.toHexString(r)); } else { buf.append(Integer.toHexString(r)); } if (g < 16) { buf.append("0").append(Integer.toHexString(g)); } else { buf.append(Integer.toHexString(g)); } if (b < 16) { buf.append("0").append(Integer.toHexString(b)); } else { buf.append(Integer.toHexString(b)); } return buf.toString().toUpperCase(); } public static String toHexString(int[] rgb) { return toHexString(rgb[0], rgb[1], rgb[2]); } }
public class HexRGBColorSample { public static void main(String[] args) { { // 白 int[] rgb = toRGB("FFFFFF"); System.out.println("255? " + rgb[0]); System.out.println("255? " + rgb[1]); System.out.println("255? " + rgb[2]); System.out.println("ffffff? " + toHexString(rgb)); } { // 黒 int[] rgb = toRGB("000000"); System.out.println("0? " + rgb[0]); System.out.println("0? " + rgb[1]); System.out.println("0? " + rgb[2]); System.out.println("000000? " + toHexString(rgb)); } { // オレンジ int[] rgb = toRGB("ffa500"); System.out.println("255? " + rgb[0]); System.out.println("165? " + rgb[1]); System.out.println("0? " + rgb[2]); System.out.println("ffa500? " + toHexString(rgb)); } } public static int[] toRGB(String hexColorRGB) { char rgb[] = hexColorRGB.toCharArray(); int[] rgbInt = new int[3]; rgbInt[0] = Character.digit(rgb[0], 16) * 16 + Character.digit(rgb[1], 16); rgbInt[1] = Character.digit(rgb[2], 16) * 16 + Character.digit(rgb[3], 16); rgbInt[2] = Character.digit(rgb[4], 16) * 16 + Character.digit(rgb[5], 16); return rgbInt; } public static String toHexString(int r, int g, int b) { StringBuffer buf = new StringBuffer(); if (r < 16) { buf.append("0").append(Integer.toHexString(r)); } else { buf.append(Integer.toHexString(r)); } if (g < 16) { buf.append("0").append(Integer.toHexString(g)); } else { buf.append(Integer.toHexString(g)); } if (b < 16) { buf.append("0").append(Integer.toHexString(b)); } else { buf.append(Integer.toHexString(b)); } return buf.toString().toUpperCase(); } public static String toHexString(int[] rgb) { return toHexString(rgb[0], rgb[1], rgb[2]); } }

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。