Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cg_lab1;
  7.  
  8. /**
  9.  *
  10.  * @author lenovo
  11.  */
  12. public class LUV {
  13.  
  14.     private javax.swing.JSlider mLUVSliderU;
  15.     private javax.swing.JSlider mLUVSliderV;
  16.     private javax.swing.JTextField mLUVTextFieldL;
  17.     private javax.swing.JTextField mLUVTextFieldU;
  18.     private javax.swing.JTextField mLUVTextFieldV;
  19.     private javax.swing.JSlider mLUVSliderL;
  20.     private RGB rgb;
  21.  
  22.     public LUV(javax.swing.JSlider mLUVSliderU,
  23.             javax.swing.JSlider mLUVSliderV,
  24.             javax.swing.JTextField mLUVTextFieldL,
  25.             javax.swing.JTextField mLUVTextFieldU,
  26.             javax.swing.JTextField mLUVTextFieldV,
  27.             javax.swing.JSlider mLUVSliderL,
  28.             RGB rgb) {
  29.         this.mLUVSliderL = mLUVSliderL;
  30.         this.mLUVSliderU = mLUVSliderU;
  31.         this.mLUVSliderV = mLUVSliderV;
  32.         this.mLUVTextFieldL = mLUVTextFieldL;
  33.         this.mLUVTextFieldU = mLUVTextFieldU;
  34.         this.mLUVTextFieldV = mLUVTextFieldV;
  35.         this.rgb = rgb;
  36.     }
  37.  
  38.     private static double[][] mToXYZ = {
  39.         {0.412453, 0.357580, 0.180423},
  40.         {0.212671, 0.715160, 0.072169},
  41.         {0.019334, 0.119193, 0.950227
  42.         }};
  43.  
  44.     private static double[][] mFromXYZ = {
  45.         {3.2406, -1.5372, -0.4986},
  46.         {-0.9689, 1.8758, 0.0415},
  47.         {0.0557, -0.2040, 1.0570}};
  48.  
  49.     private double funcRGBtoXYZ(double x) {
  50.         if (x > 0.04045) {
  51.             return Math.pow((x + 0.055) / 1.055, 2.4);
  52.         }
  53.         return x / 12.92;
  54.  
  55.     }
  56.  
  57.     private double funcXYZtoRGB(double x) {
  58.         if (x > 0.0031308) {
  59.             return 1.055 * Math.pow(x, 1d / 2.4) - 0.055;
  60.         }
  61.         return x * 12.92;
  62.     }
  63.  
  64.     private double funcXYZtoLUV(double x) {
  65.         if (x > 0.008856) {
  66.             return Math.pow(x, 1d / 3);
  67.         }
  68.         return 7.7787 * x + 16d / 116;
  69.     }
  70.  
  71.     private double funcLUVtoXYZ(double x) {
  72.         if (x > 0.008856) {//??
  73.             return Math.pow(x, 3);
  74.         }
  75.         return (x - 16d / 116) / 7.7787;
  76.     }
  77.  
  78.     private static double[] dot(double[][] matrix, double[] vector) {
  79.         double[] ret = new double[3];
  80.         for (int i = 0; i < 3; ++i) {
  81.             ret[i] = 0;
  82.             for (int j = 0; j < 3; ++j) {
  83.                 ret[i] += matrix[i][j] * vector[j];
  84.             }
  85.         }
  86.         return ret;
  87.     }
  88.  
  89.     private static double[] whitePoint = {95.047, 100, 108.883};
  90.  
  91.     private double[] toLUV() {
  92.         double[] luv = new double[3];
  93.  
  94.         double[] rgbn = new double[3];
  95.         rgbn[0] = funcRGBtoXYZ(rgb.getR()) * 100;
  96.         rgbn[1] = funcRGBtoXYZ(rgb.getG()) * 100;
  97.         rgbn[2] = funcRGBtoXYZ(rgb.getB()) * 100;
  98.         double[] xyz = dot(mToXYZ, rgbn);
  99.         //System.out.println("to X = " + xyz[0] + ", Y = " + xyz[1] + ", Z = " + xyz[2]);
  100.         if (rgbn[0] == 0 && rgbn[1] == 0 && rgbn[2] == 0) {
  101.             return luv;
  102.         }
  103.  
  104.         double u_ = 4d * xyz[0] / (xyz[0] + 15 * xyz[1] + 3 * xyz[2]);
  105.         double v_ = 9d * xyz[1] / (xyz[0] + 15 * xyz[1] + 3 * xyz[2]);
  106.         double uw_ = 4d * whitePoint[0] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
  107.         double vw_ = 9d * whitePoint[1] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
  108.  
  109.         luv[0] = 116d * funcXYZtoLUV(xyz[1] / whitePoint[1]) - 16;
  110.         luv[1] = 13 * luv[0] * (u_ - uw_);
  111.         luv[2] = 13 * luv[0] * (v_ - vw_);
  112.  
  113.         //toRGB(luv[0], luv[1], luv[2]);
  114.         return luv;
  115.     }
  116.  
  117.     private void toRGB(double l, double u, double v) {
  118.         double uw_ = 4d * whitePoint[0] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
  119.         double vw_ = 9d * whitePoint[1] / (whitePoint[0] + 15 * whitePoint[1] + 3 * whitePoint[2]);
  120.  
  121.         double u_ = u / (13d * l) + uw_;
  122.         double v_ = v / (13d * l) + vw_;
  123.  
  124.         double[] xyz = new double[3];
  125.         xyz[1] = funcLUVtoXYZ((l + 16) / 116) * whitePoint[1];
  126.         xyz[0] = -(9 * xyz[1] * u_) / ((u_ - 4) * v_ - u_ * v_);
  127.         xyz[2] = (9 * xyz[1] - (15 * v_ * xyz[1] + (v_ * xyz[0]))) / (3 * v_);
  128.  
  129.         for (int i = 0; i < 3; ++i) {
  130.             xyz[i] /= 100;
  131.         }
  132.         //System.out.println("from X = " + xyz[0] + ", Y = " + xyz[1] + ", Z = " + xyz[2]);
  133.  
  134.         double[] rgbn = dot(mFromXYZ, xyz);
  135.         rgb.setR((float) funcXYZtoRGB(rgbn[0]));
  136.         rgb.setG((float) funcXYZtoRGB(rgbn[1]));
  137.         rgb.setB((float) funcXYZtoRGB(rgbn[2]));
  138.     }
  139.  
  140.     public void updateAll() {
  141.         double[] luv = toLUV();
  142.         mLUVTextFieldL.setText(String.format("%.0f", luv[0]));
  143.         mLUVTextFieldU.setText(String.format("%.0f", luv[1]));
  144.         mLUVTextFieldV.setText(String.format("%.0f", luv[2]));
  145.         mLUVSliderL.setValue((int) Math.round(luv[0]));
  146.         mLUVSliderU.setValue((int) Math.round(luv[1]));
  147.         mLUVSliderV.setValue((int) Math.round(luv[2]));
  148.     }
  149.  
  150.     public void textChanged() {
  151.         double l = (double) mLUVSliderL.getValue();
  152.         double u = (double) mLUVSliderU.getValue();
  153.         double v = (double) mLUVSliderV.getValue();
  154.         try {
  155.             l = Double.parseDouble(mLUVTextFieldL.getText());
  156.             u = Double.parseDouble(mLUVTextFieldU.getText());
  157.             v = Double.parseDouble(mLUVTextFieldV.getText());
  158.             if (l >= mLUVSliderL.getMinimum() && l <= mLUVSliderL.getMaximum()
  159.                     && u >= mLUVSliderU.getMinimum() && u <= mLUVSliderU.getMaximum()
  160.                     && v >= mLUVSliderV.getMinimum() && v <= mLUVSliderV.getMaximum()) {
  161.                 toRGB(l, u, v);
  162.                 mLUVSliderL.setValue((int) Math.round(l));
  163.                 mLUVSliderU.setValue((int) Math.round(u));
  164.                 mLUVSliderV.setValue((int) Math.round(v));
  165.             }
  166.         } catch (Exception e) {
  167.             e.printStackTrace();
  168.             mLUVTextFieldL.setText(String.format("%.0f", l));
  169.             mLUVTextFieldU.setText(String.format("%.0f", u));
  170.             mLUVTextFieldV.setText(String.format("%.0f", v));
  171.         }
  172.  
  173.     }
  174.  
  175.     public void sliderChanged() {
  176.         double l = (double) mLUVSliderL.getValue();
  177.         double u = (double) mLUVSliderU.getValue();
  178.         double v = (double) mLUVSliderV.getValue();
  179.         toRGB(l, u, v);
  180.         mLUVTextFieldL.setText(String.format("%.0f", l));
  181.         mLUVTextFieldU.setText(String.format("%.0f", u));
  182.         mLUVTextFieldV.setText(String.format("%.0f", v));
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement