从RGB转换为HSV和CMYK时崩溃

时间:2015-08-08 02:56:24

标签: java android

我是Android开发的新手,我正在尝试从RGB转换为HSV和CMYK。我有以下两种方法来转换值:

public void convertHSV() {
    android.graphics.Color.RGBToHSV(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor), hsvColor);
    TextView hValue = (TextView) findViewById(R.id.hValue);
    TextView sValue = (TextView) findViewById(R.id.sValue);
    TextView vValue = (TextView) findViewById(R.id.vValue);

    hValue.setText(String.valueOf(hsvColor[1]));
    sValue.setText(String.valueOf(hsvColor[2]));
    vValue.setText(String.valueOf(hsvColor[3]));
}

public void convertCMYK() {
    String CValue;
    String MValue;
    String YValue;
    String KValue;

    int computedC;
    int computedM;
    int computedY;
    int computedK;
    int minCMY;

    TextView cyanValue = (TextView) findViewById(R.id.cyanValue);
    TextView magentaValue = (TextView) findViewById(R.id.magentaValue);
    TextView yellowValue = (TextView) findViewById(R.id.yellowValue);
    TextView blackValue = (TextView) findViewById(R.id.blackValue);


    if (hexRed == "00" && hexGreen == "00" && hexBlue == "00") {

        CValue = "0";
        MValue = "0";
        YValue = "0";
        KValue = "1";

    } else {
        computedC = 1 - (Integer.parseInt(hexRed) / 255);
        computedM = 1 - (Integer.parseInt(hexGreen) / 255);
        computedY = 1 - (Integer.parseInt(hexBlue) / 255);

        minCMY = Math.min(computedC, Math.min(computedM, computedY));

        computedC = (computedC - minCMY) / (1 - minCMY);
        computedM = (computedM - minCMY) / (1 - minCMY);
        computedY = (computedY - minCMY) / (1 - minCMY);
        computedK = minCMY;

        CValue = Integer.toString(computedC);
        MValue = Integer.toString(computedM);
        YValue = Integer.toString(computedY);
        KValue = Integer.toString(computedK);
    }

    cyanValue.setText(CValue);
    magentaValue.setText(MValue);
    yellowValue.setText(YValue);
    blackValue.setText(KValue);
}

我更改每个滑块时调用这些方法:

 SeekBar blueBar = (SeekBar) findViewById(R.id.blueBar);

    final TextView blueBarValue = (TextView) findViewById(R.id.blueValue);

    blueBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar blueBar, int progress, boolean fromUser) {
            blueBarValue.setText(String.valueOf(progress));
            bgColor = Color.rgb(Color.red(bgColor), Color.green(bgColor), progress);
            hexBlue = Integer.toHexString(progress);
            if (hexBlue.length() == 1) {
                hexBlue = "0" + hexBlue;
            }
            updateBackgroundColor();
            updateHexValue();
            convertHSV();
            //convertCMYK();
        }

        @Override
        public void onStartTrackingTouch(SeekBar blueBarValue) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar blueBarValue) {

        }
    });

当我使用滑块时,应用程序只会在模拟器中崩溃。我尝试只在用户放开滑块时调用它们,但它仍然导致它崩溃。为什么会这样?

0 个答案:

没有答案