convert RGB Color to YUV - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

convert RGB Color to YUV

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int in = 2;
        System.out.println(convRGBtoYUV(in));
    }//from   w w w  .  j  ava 2s.  c  om

    public static int convRGBtoYUV(int in) { //http://softpixel.com/~cwright/programming/colorspace/yuv/
        int r = getR(in);
        int g = getG(in);
        int b = getB(in);

        int y = (int) Math.round(r * .299000 + g * .587000 + b * .114000);
        int u = (int) Math.round(r * -.168736 + g * -.331264 + b * .500000
                + 128);
        int v = (int) Math.round(r * .500000 + g * -.418688 + b * -.081312
                + 128);
        return ((y & 0xFF) << 16) + ((u & 0xFF) << 8) + (v & 0xFF);
    }

    public static int convRGBtoYUV(int r, int g, int b) { //http://softpixel.com/~cwright/programming/colorspace/yuv/ just an alternate form
        int y = (int) Math.round(r * .299000 + g * .587000 + b * .114000);
        int u = (int) Math.round(r * -.168736 + g * -.331264 + b * .500000
                + 128);
        int v = (int) Math.round(r * .500000 + g * -.418688 + b * -.081312
                + 128);
        return ((y & 0xFF) << 16) + ((u & 0xFF) << 8) + (v & 0xFF);
    }

    public static int getR(int col) { //N.B. BGR?
        return (col >> 16) & 0xFF;
    }

    public static int getG(int col) {
        return (col >> 8) & 0xFF;
    }

    public static int getB(int col) {
        return (col & 0xFF);
    }
}

Related Tutorials