【问题标题】:java - find h, s, and b of hsb colorjava - 查找hsb颜色的h、s和b
【发布时间】:2014-01-10 22:50:08
【问题描述】:

我正在制作一个 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 文件吗?如果是这样的话,我不知道该怎么做。

【问题讨论】:

    标签: java colors applet


    【解决方案1】:

    你可以使用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

    【讨论】:

    • 所以颜色颜色等于openhsb?
    • 是的,您可以只存储一个Color 对象,当您需要获取hsb 时,您可以使用RGBtoHSB 方法对其进行转换。但是,如果您需要经常访问 hsb 值(可能在绘图线程中),那么您可以存储这些值并在更改颜色时更新它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2021-07-09
    • 2020-11-03
    • 2022-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多