Java Swing issue - Using color palette

前端 未结 5 1933
南旧
南旧 2020-12-04 00:58

I have a problem here - I have a hexadecimal value being stored in a textfield after I have selected a color (using JColorChooser). What I would like to do is display the

5条回答
  •  醉酒成梦
    2020-12-04 01:08

    RGB is not a very best color model to work with in this situation. HSB would be more appropriate.

    1. Convert RGB to HSB:

      float[] hsb = Color.RGBtoHSB(red, green, blue, null);
      
    2. Detect color:

      String color;
      
      if (hsb[0] >= 0.0 && ksb[0] <= 0.1) {
          color = "Red";
      } else if (hsb[0] > 0.1 && ksb[0] <= 0.25) {
          color = "Orange";
      } else if (hsb[0] > 0.25 && ksb[0] <= 0.35) {
          color = "Yellow";
      } ...
      

提交回复
热议问题