java - 找到hsb颜色的h,s和b

时间:2014-01-10 22:50:09

标签: java colors applet

我正在制作HSB颜色选择器。您可以输入颜色的色调,饱和度和亮度,然后输入您输入的颜色的小方块。现在我允许用户保存颜色,但是我想要它,这样当你按下“打开颜色”按钮时,h,s和b文本输入框会显示h,s和b的值。保存的颜色。这是我的代码:

@SuppressWarnings("serial")
public class Main extends Applet implements ActionListener, java.io.Serializable {
private Rectangle color;
Random randomColor = new Random();
TextField one;
TextField two;
TextField three;
Button enter;
Button random;
Button save;
Button open;
String hvalue="255";
String svalue="0";
String bvalue="255";
Color hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));

int width = 320;
int height = 260;
String version = "0.0.1";
String name = "Color Picker"; 


public void init() {
    setSize(width, height); 
    Frame c = (Frame)this.getParent().getParent();
    c.setTitle(name + " - Version " + version);
    setLayout(null);
    color = new Rectangle(width/2-32, 20, 64, 64);

    one = new TextField("", 15);
    one.setBounds(width/2-60, height-120, 40, 20);
    add(one);
    two = new TextField("", 15);
    two.setBounds(width/2-20, height-120, 40, 20);
    add(two);
    three = new TextField("", 15);
    three.setBounds(width/2+20, height-120, 40, 20);
    add(three);
    enter = new Button("Enter");
    enter.setBounds(width/2-50, height-80, 100, 20);
    add(enter);
    enter.addActionListener(this);
    random = new Button("Random Color");
    random.setBounds(width/2-50, height-60, 100, 20);
    add(random);
    random.addActionListener(this);
    save = new Button("Save Color");
    save.setBounds(width/2-50, height-40, 100, 20);
    add(save);
    save.addActionListener(this);
    open = new Button("Open Color");
    open.setBounds(width/2-50, height-20, 100, 20);
    add(open);
    open.addActionListener(this);
}


public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(hsb);
    g2.fill(color);
}


@Override
public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == enter) {
        hvalue = one.getText();
        svalue = two.getText();
        bvalue = three.getText();
        try {
            hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));
            repaint();
        }
        catch (Exception e) {}
    }

    else if (evt.getSource() == random) {
        hvalue = String.valueOf(randomColor.nextInt(200));
        svalue = String.valueOf(randomColor.nextInt(200));
        bvalue = String.valueOf(randomColor.nextInt(200));
        one.setText(hvalue);
        two.setText(svalue);
        three.setText(bvalue);
        try {
            hsb = Color.getHSBColor(Integer.parseInt(hvalue), Integer.parseInt(bvalue), Integer.parseInt(svalue));
            repaint();
        }
        catch (Exception e) {}
    }

    else if (evt.getSource() == save) {
        try {
            FileOutputStream fileOut = new FileOutputStream("/C:/Users/Barbara/Downloads/color.color");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(hsb);
            out.close();
            fileOut.close();
        } catch(IOException i) {
            i.printStackTrace();
        }
    }

    else if (evt.getSource() == open) {
        Color openhsb = null;
        try {
            FileInputStream fileIn = new FileInputStream("/C:/Users/Barbara/Downloads/color.color");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            openhsb = (Color) in.readObject();
            in.close();
            fileIn.close();
            try {
                hsb = openhsb;
                repaint();
            }
            catch (Exception e) {}
        } catch(IOException i) {
            i.printStackTrace();
            return;
        } catch(ClassNotFoundException c) {
            c.printStackTrace();
            return;
        }
    }

}
}

我应该将h,s和b值保存到color1.color文件吗?我不知道如果是这样的话。

1 个答案:

答案 0 :(得分:0)

您可以使用Color类提供的转换器:

Color color = Color.red;
float [] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);

Color color = Color.red;
float [] hsb = new float[3];
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);

重新调整的值介于0和1之间。因此,您可能希望将它们乘以360,以使它们适合您的程序模型。

Javadoc for RGBtoHSB