Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package cg_lab1;
- /**
- *
- * @author lenovo
- */
- public class LUV {
- private javax.swing.JSlider mLUVSliderU;
- private javax.swing.JSlider mLUVSliderV;
- private javax.swing.JTextField mLUVTextFieldL;
- private javax.swing.JTextField mLUVTextFieldU;
- private javax.swing.JTextField mLUVTextFieldV;
- private javax.swing.JSlider mLUVSliderL;
- private RGB rgb;
- public LUV(javax.swing.JSlider mLUVSliderU,
- javax.swing.JSlider mLUVSliderV,
- javax.swing.JTextField mLUVTextFieldL,
- javax.swing.JTextField mLUVTextFieldU,
- javax.swing.JTextField mLUVTextFieldV,
- javax.swing.JSlider mLUVSliderL,
- RGB rgb) {
- this.mLUVSliderL = mLUVSliderL;
- this.mLUVSliderU = mLUVSliderU;
- this.mLUVSliderV = mLUVSliderV;
- this.mLUVTextFieldL = mLUVTextFieldL;
- this.mLUVTextFieldU = mLUVTextFieldU;
- this.mLUVTextFieldV = mLUVTextFieldV;
- this.rgb = rgb;
- }
- private static double[][] mToXYZ = {
- {0.412453, 0.357580, 0.180423},
- {0.212671, 0.715160, 0.072169},
- {0.019334, 0.119193, 0.950227
- }};
- private static double[][] mFromXYZ = {
- {3.2406, -1.5372, -0.4986},
- {-0.9689, 1.8758, 0.0415},
- {0.0557, -0.2040, 1.0570}};
- private double funcRGBtoXYZ(double x) {
- if (x > 0.04045) {
- return Math.pow((x + 0.055) / 1.055, 2.4);
- }
- return x / 12.92;
- }
- private double funcXYZtoRGB(double x) {
- if (x > 0.0031308) {
- return 1.055 * Math.pow(x, 1d / 2.4) - 0.055;
- }
- return x * 12.92;
- }
- private double funcXYZtoLUV(double x) {
- if (x > 0.008856) {
- return Math.pow(x, 1d / 3);
- }
- return 7.7787 * x + 16d / 116;
- }
- private double funcLUVtoXYZ(double x) {
- if (x > 0.008856) {//??
- return Math.pow(x, 3);
- }
- return (x - 16d / 116) / 7.7787;
- }
- private static double[] dot(double[][] matrix, double[] vector) {
- double[] ret = new double[3];
- for (int i = 0; i < 3; ++i) {
- ret[i] = 0;
- for (int j = 0; j < 3; ++j) {
- ret[i] += matrix[i][j] * vector[j];
- }
- }
- return ret;
- }
- private static double[] whitePoint = {95.047, 100, 108.883};
- private double[] toLUV() {
- double[] luv = new double[3];
- double[] rgbn = new double[3];
- rgbn[0] = funcRGBtoXYZ(rgb.getR()) * 100;
- rgbn[1] = funcRGBtoXYZ(rgb.getG()) * 100;
- rgbn[2] = funcRGBtoXYZ(rgb.getB()) * 100;
- double[] xyz = dot(mToXYZ, rgbn);
- //System.out.println("to X = " + xyz[0] + ", Y = " + xyz[1] + ", Z = " + xyz[2]);
- if (rgbn[0] == 0 && rgbn[1] == 0 && rgbn[2] == 0) {
- return luv;
- }
- double u_ = 4d * xyz[0] / (xyz[0] + 15 * xyz[1] + 3 * xyz[2]);
- double v_ = 9d * xyz[1] / (xyz[0] + 15 * xyz[1] + 3 * xyz[2]);
- double uw_ = 4d * whitePoint[0] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
- double vw_ = 9d * whitePoint[1] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
- luv[0] = 116d * funcXYZtoLUV(xyz[1] / whitePoint[1]) - 16;
- luv[1] = 13 * luv[0] * (u_ - uw_);
- luv[2] = 13 * luv[0] * (v_ - vw_);
- //toRGB(luv[0], luv[1], luv[2]);
- return luv;
- }
- private void toRGB(double l, double u, double v) {
- double uw_ = 4d * whitePoint[0] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
- double vw_ = 9d * whitePoint[1] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
- double u_ = u / (13d * l) + uw_;
- double v_ = v / (13d * l) + vw_;
- double[] xyz = new double[3];
- xyz[1] = funcLUVtoXYZ((l + 16) / 116) * whitePoint[1];
- xyz[0] = -(9 * xyz[1] * u_) / ((u_ - 4) * v_ - u_ * v_);
- xyz[2] = (9 * xyz[1] - (15 * v_ * xyz[1] + (v_ * xyz[0]))) / (3 * v_);
- for (int i = 0; i < 3; ++i) {
- xyz[i] /= 100;
- }
- //System.out.println("from X = " + xyz[0] + ", Y = " + xyz[1] + ", Z = " + xyz[2]);
- double[] rgbn = dot(mFromXYZ, xyz);
- rgb.setR((float) funcXYZtoRGB(rgbn[0]));
- rgb.setG((float) funcXYZtoRGB(rgbn[1]));
- rgb.setB((float) funcXYZtoRGB(rgbn[2]));
- }
- public void updateAll() {
- double[] luv = toLUV();
- mLUVTextFieldL.setText(String.format("%.0f", luv[0]));
- mLUVTextFieldU.setText(String.format("%.0f", luv[1]));
- mLUVTextFieldV.setText(String.format("%.0f", luv[2]));
- mLUVSliderL.setValue((int) Math.round(luv[0]));
- mLUVSliderU.setValue((int) Math.round(luv[1]));
- mLUVSliderV.setValue((int) Math.round(luv[2]));
- }
- public void textChanged() {
- double l = (double) mLUVSliderL.getValue();
- double u = (double) mLUVSliderU.getValue();
- double v = (double) mLUVSliderV.getValue();
- try {
- l = Double.parseDouble(mLUVTextFieldL.getText());
- u = Double.parseDouble(mLUVTextFieldU.getText());
- v = Double.parseDouble(mLUVTextFieldV.getText());
- if (l >= mLUVSliderL.getMinimum() && l <= mLUVSliderL.getMaximum()
- && u >= mLUVSliderU.getMinimum() && u <= mLUVSliderU.getMaximum()
- && v >= mLUVSliderV.getMinimum() && v <= mLUVSliderV.getMaximum()) {
- toRGB(l, u, v);
- mLUVSliderL.setValue((int) Math.round(l));
- mLUVSliderU.setValue((int) Math.round(u));
- mLUVSliderV.setValue((int) Math.round(v));
- }
- } catch (Exception e) {
- e.printStackTrace();
- mLUVTextFieldL.setText(String.format("%.0f", l));
- mLUVTextFieldU.setText(String.format("%.0f", u));
- mLUVTextFieldV.setText(String.format("%.0f", v));
- }
- }
- public void sliderChanged() {
- double l = (double) mLUVSliderL.getValue();
- double u = (double) mLUVSliderU.getValue();
- double v = (double) mLUVSliderV.getValue();
- toRGB(l, u, v);
- mLUVTextFieldL.setText(String.format("%.0f", l));
- mLUVTextFieldU.setText(String.format("%.0f", u));
- mLUVTextFieldV.setText(String.format("%.0f", v));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement