2010-08-31 13 views
57

में परिवर्तित करें मेरे जावा एप्लिकेशन में, मैं लाल, हरे, नीले मानों के संदर्भ में JButton रंग का मान प्राप्त करने में सक्षम था; मैंने इन मानों को तीन पूर्णांक में संग्रहीत किया है।एक आरजीबी रंग मान को हेक्साडेसिमल

आरजीबी मूल्यों को समकक्ष हेक्साडेसिमल मान में कैसे परिवर्तित करें? की तरह इस प्रारूप में #0033fA

+4

हेक्साडेसिमल मान जैसी कोई चीज़ नहीं है। हेक्साडेसिमल * प्रतिनिधित्व है। * – EJP

उत्तर

142

कि

उदाहरण आप अपने जिसके परिणामस्वरूप हेक्स अंक बड़े अक्षरों में होना (#FFFFFF बनाम #ffffff) चाहते हैं तो आप

String hex = String.format("#%02x%02x%02x", r, g, b); 

राजधानी उपयोग एक्स के उपयोग कर सकते हैं।

+1

और यह वापस रास्ता है: http://stackoverflow.com/a/4129692/1761622 – Mikescher

+0

धन्यवाद @ mhshams –

12
Random ra = new Random(); 
int r, g, b; 
r=ra.nextInt(255); 
g=ra.nextInt(255); 
b=ra.nextInt(255); 
Color color = new Color(r,g,b); 
String hex = Integer.toHexString(color.getRGB() & 0xffffff); 
if (hex.length() < 6) { 
    hex = "0" + hex; 
} 
hex = "#" + hex; 
+0

यह उत्तर इस मामले में विफल रहता है कि लाल या हरे रंग के मान शून्य होते हैं (एक उदाहरण 'रंग। BLUE' होता है, जो' # 0ff' आउटपुट करता है क्योंकि रंग के आरजीबी मान को 'बीएलयूई परिणाम' बेस 10 में '256' में, जो हेक्स में 'एफएफ' है)। शून्य को प्रीरेंड करते समय एक कथन के बजाय 'while' लूप का उपयोग करना एक फिक्स है। – Vulcan

35

एक एक लाइनर लेकिन String.Format बिना:

Color your_color = Color.BLACK; 

String hex = "#"+Integer.toHexString(your_color.getRGB()).substring(2); 

आप एक .toUpperCase() जोड़ सकते हैं यदि आप बड़े अक्षर पर स्विच करना चाहते हैं।

+6

ध्यान रखें कि यदि आपके रंग में अल्फा मान है तो यह विधि टूट गई है <16 (यानी इसका हेक्साडेसिमल एआरजीबी प्रतिनिधित्व शुरू होता है 0)। – ARRG

1

यह से अपडेट के साथ Vivien Barousse द्वारा दिए गए उत्तर का एक अनुकूलित संस्करण है। इस उदाहरण में मैं स्लाइडर का उपयोग तीन स्लाइडर से आरजीबी मूल्यों को गतिशील रूप से पुनः प्राप्त करने और आयत में उस रंग को प्रदर्शित करने के लिए करता हूं। फिर विधि में हेक्स() में मैं रंग बनाने और संबंधित हेक्स रंग कोड प्रदर्शित करने के लिए मानों का उपयोग करता हूं।

इस उदाहरण में ग्रिडबैगलाउट के लिए उचित बाधाएं शामिल नहीं हैं। हालांकि कोड काम करेगा, प्रदर्शन अजीब लगेगा।

public class HexColor 
{ 

    public static void main (String[] args) 
    { 
    JSlider sRed = new JSlider(0,255,1); 
    JSlider sGreen = new JSlider(0,255,1); 
    JSlider sBlue = new JSlider(0,255,1); 
    JLabel hexCode = new JLabel(); 
    JPanel myPanel = new JPanel(); 
    GridBagLayout layout = new GridBagLayout(); 
    JFrame frame = new JFrame(); 

    //set frame to organize components using GridBagLayout 
    frame.setLayout(layout); 

    //create gray filled rectangle 
    myPanel.paintComponent(); 
    myPanel.setBackground(Color.GRAY); 

    //In practice this code is replicated and applied to sGreen and sBlue. 
    //For the sake of brevity I only show sRed in this post. 
    sRed.addChangeListener(
     new ChangeListener() 
     { 
      @Override 
      public void stateChanged(ChangeEvent e){ 
       myPanel.setBackground(changeColor()); 
       myPanel.repaint(); 
       hexCode.setText(toHex()); 
     } 
     } 
    ); 
    //add each component to JFrame 
    frame.add(myPanel); 
    frame.add(sRed); 
    frame.add(sGreen); 
    frame.add(sBlue); 
    frame.add(hexCode); 
} //end of main 

    //creates JPanel filled rectangle 
    protected void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     g.drawRect(360, 300, 10, 10); 
     g.fillRect(360, 300, 10, 10); 
    } 

    //changes the display color in JPanel 
    private Color changeColor() 
    { 
    int r = sRed.getValue(); 
    int b = sBlue.getValue(); 
    int g = sGreen.getValue(); 
    Color c; 
    return c = new Color(r,g,b); 
    } 

    //Displays hex representation of displayed color 
    private String toHex() 
    { 
     Integer r = sRed.getValue(); 
     Integer g = sGreen.getValue(); 
     Integer b = sBlue.getValue(); 
     Color hC; 
     hC = new Color(r,g,b); 
     String hex = Integer.toHexString(hC.getRGB() & 0xffffff); 
     while(hex.length() < 6){ 
      hex = "0" + hex; 
     } 
     hex = "Hex Code: #" + hex; 
     return hex; 
    } 
} 

एक विशाल दोनों विविएन और वल्कन लिए धन्यवाद। यह समाधान पूरी तरह से काम करता है और लागू करने के लिए बहुत आसान था।

संबंधित मुद्दे