/*
 * 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 com.seniorproject.augmentedreality.utils;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *
 * @author RainWhileLoop
 */
public class ConvertColor {

    Integer width;
    Integer height;

    private float h = 0.0f;
    private float s = 0.0f;
    private float v = 0.0f;

    public static void main(String[] args) {
        new ConvertColor();
    }

    public ConvertColor() {

        File file = new File("E:\\dog.jpg");
        if (!file.isFile()) {
            System.err.print("Error");
        }
        InputStream stream = null;
        try {
            stream = new FileInputStream(file);
            BufferedImage image = ImageIO.read(stream);
            getSize(image);
        } catch (IOException ex) {
            Logger.getLogger(ConvertColor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                stream.close();
            } catch (IOException ex) {
                Logger.getLogger(ConvertColor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        JFrame frame = new JFrame();
        Canvas canvas = new Canvas();
        canvas.setBackground(Color.getHSBColor(this.h, this.s, this.v));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(canvas);
        frame.setSize(300, 300);
        frame.setLocation(500, 500);
        frame.setTitle("Test Convert Color");
        frame.setVisible(true);
    }

    private void getSize(BufferedImage image) {
        this.width = image.getWidth();
        this.height = image.getHeight();
        System.out.println("width = " + width);
        System.out.println("height = " + height);
//        for (int x = 0; x < width; x++) {
//            for (int y = 0; y < height; y++) {
        printPixelARGB(image.getRGB(105, 44));
//            }
//        }
    }

    private void printPixelARGB(Integer pixel) {
        System.out.println("bit pixel : " + pixel.toBinaryString(pixel));
        System.out.println("count bit pixel : " + pixel.toBinaryString(pixel).length());
        Integer alpha = (pixel >> 24) & 0xff;
        Integer red = (pixel >> 16) & 0xff;
        Integer green = (pixel >> 8) & 0xff;
        Integer blue = (pixel) & 0xff;
//        System.out.println("alpha : " + alpha);
        System.out.println("red : " + red.toBinaryString(red) + " (" + red + ")");
        System.out.println("green : " + green.toBinaryString(green) + " (" + green + ")");
        System.out.println("blue : " + blue.toBinaryString(blue) + " (" + blue + ")");
        RGBtoHSV(red, green, blue);
    }

    private void RGBtoHSV(int r, int g, int b) {

        Float fr = r / 255.0f;
        Float fg = g / 255.0f;
        Float fb = b / 255.0f;
        float max = Math.max(Math.max(fr, fg), fb);
        float min = Math.min(Math.min(fr, fg), fb);
        this.v = max;
        float delta = max - min;
        this.s = (max == 0.0f) ? 0.0f : delta / max;
        if (fr.equals(max)) {
            this.h = 60.0f * ((fg - fb) / delta + (fg < fb ? 6 : 0));
        } else if (fg.equals(max)) {
            this.h = 60.0f * ((fb - fr) / delta + 2);
        } else {
            this.h = 60.0f * ((fr - fg) / delta + 4);
        }

        System.out.println("Hue : " + this.h);
        System.out.println("Saturate : " + this.s);
        System.out.println("Value : " + this.v);
    }
}

@@ -1557,398 +1557,25 @@
  %7D%0D%0A
-        JFrame frame = new JFrame();%0D%0A        Canvas canvas = new Canvas();%0D%0A        canvas.setBackground(Color.getHSBColor(this.h, this.s, this.v));%0D%0A        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);%0D%0A        frame.add(canvas);%0D%0A        frame.setSize(300, 300);%0D%0A        frame.setLocation(500, 500);%0D%0A        frame.setTitle(%22Test Convert Color%22);%0D%0A        frame.setVisible(true
+%0D%0A        getGUI(
 );%0D%0A
@@ -1949,15 +1949,15 @@
 RGB(
-105, 44
+377, 87
 ));%0D
@@ -3553,12 +3553,475 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    private void getGUI() %7B%0D%0A        JFrame frame = new JFrame();%0D%0A        Canvas canvas = new Canvas();%0D%0A        canvas.setBackground(Color.getHSBColor(this.h, this.s, this.v));%0D%0A        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);%0D%0A        canvas.setSize(250, 250);%0D%0A        frame.add(canvas);%0D%0A        frame.setSize(800, 600);%0D%0A        frame.setLocation(0, 0);%0D%0A        frame.setTitle(%22Test Convert Color%22);%0D%0A        frame.setVisible(true);%0D%0A    %7D%0D
 %0A%7D%0D%0A

add GUI to see color