2013-07-29 60 views

回答

4

該做的伎倆:

// 8 symbols. 
String hex1 = Integer.toHexString(myColorPicker.getValue().hashCode()); 

// With # prefix. 
String hex2 = "#" + Integer.toHexString(myColorPicker.getValue().hashCode()); 

// 6 symbols in capital letters. 
String hex3 = Integer.toHexString(myColorPicker.getValue().hashCode()).substring(0, 6).toUpperCase(); 
+1

這很容易,做得很好。 – Marc

+3

這是一個脆弱的解決方案。哈希代碼的生成依賴於實現,所以不能保證在以後的版本中它可以工作。 (例如,如果另一個字段包含在散列碼中,例如opacy。) – Balage1551

+1

不錯的工作,這對我有幫助 –

2

您可以使用getGreen(),getBlue(),getRed()方法並將其轉換爲十六進制。

Color c; 
    int green = c.getGreen()*255; 
    Integer.toHexString(green); 

重複此爲紅色和藍色,則:

String hexColor = "#"+red+green+blue; 

這是理念,完整的代碼(複製pastable):

public class TestColor { 

     public TestColor() { 
      Color c = Color.ALICEBLUE; 

      int green = (int) (c.getGreen()*255); 
      String greenString = Integer.toHexString(green); 

      int red = (int) (c.getRed()*255); 
      String redString = Integer.toHexString(red); 

      int blue = (int) (c.getBlue()*255); 
      String blueString = Integer.toHexString(blue); 

      String hexColor = "#"+redString+greenString+blueString; 
      System.out.println(hexColor); 
      System.out.println(c.toString()); 
     } 
     public static void main(String[] args) { 
      new TestColor(); 
     } 
    } 
+0

這不工作。如果任何組件爲零,它將只是'0'而不是'00' –

28

將顏色轉換爲網頁顏色代碼:

public class FxUtils 
{ 
    public static String toRGBCode(Color color) 
    { 
     return String.format("#%02X%02X%02X", 
      (int)(color.getRed() * 255), 
      (int)(color.getGreen() * 255), 
      (int)(color.getBlue() * 255)); 
    } 
} 
+0

這裏的答案中的唯一變體,對於黑色和其他簡單的顏色,沒有例外,工作正常。 – Dragon

0

我認爲我有一個更好的解決方案。

希望它有幫助。

import javafx.scene.paint.Color; 

/** 
* 
* @author Marcos Martinewski Alves 
*/ 
public class ColorUtils { 

    public static String colorToHex(Color color) { 
     return colorChanelToHex(color.getRed()) 
       + colorChanelToHex(color.getGreen()) 
       + colorChanelToHex(color.getBlue()) 
       + colorChanelToHex(color.getOpacity()); 
    } 

    private static String colorChanelToHex(double chanelValue) { 
     String rtn = Integer.toHexString((int) Math.min(Math.round(chanelValue * 255), 255)); 
     if (rtn.length() == 1) { 
      rtn = "0" + rtn; 
     } 
     return rtn; 
    } 

} 
2

return String.format("#%02X%02X%02X", 
    ((int)color.getRed())*255, 
    ((int)color.getGreen())*255, 
    ((int)color.getBlue())*255); 

目前可用的那些中大多數工作答案目前公認的答案是Zon的公司(以下僅供參考)

// 8 symbols. 
    String hex1 = Integer.toHexString(myColorPicker.getValue().hashCode()); 

// With # prefix. 
    String hex2 = "#" + Integer.toHexString(myColorPicker.getValue().hashCode()); 

// 6 symbols in capital letters. 
    String hex3 = Integer.toHexString(myColorPicker.getValue().hashCode()).substring(0, 6).toUpperCase(); 

但是這種方法運行到的問題自動刪除起始零。如果顏色的十六進制值以0開頭(例如#000000,#00A3FF等),則開始的零將自動刪除,而字符串太短而無法完全作爲十六進制代碼運行。 Color.BLACK會生成十六進制「#FF」,因爲它只保留其不透明度。下面的方法,從JavaFX 8u112完全解決顏色到十六進制轉換。

String colorToHex(Color color) { 
    String hex1; 
    String hex2; 

    hex1 = Integer.toHexString(color.hashCode()).toUpperCase(); 

    switch (hex1.length()) { 
    case 2: 
     hex2 = "000000"; 
     break; 
    case 3: 
     hex2 = String.format("00000%s", hex1.substring(0,1)); 
     break; 
    case 4: 
     hex2 = String.format("0000%s", hex1.substring(0,2)); 
     break; 
    case 5: 
     hex2 = String.format("000%s", hex1.substring(0,3)); 
     break; 
    case 6: 
     hex2 = String.format("00%s", hex1.substring(0,4)); 
     break; 
    case 7: 
     hex2 = String.format("0%s", hex1.substring(0,5)); 
     break; 
    default: 
     hex2 = hex1.substring(0, 6); 
    } 
    return hex2; 
} 

希望這節省了一些人我遇到的麻煩!

+0

您的第一個解決方案在每個Java版本上只產生零,因爲鑄造具有更高的優先級,因此您基本上總是將「0 * 255」 – Midnightas