Java ByteBuffer Get getHSBfromRGB(final ByteBuffer pixels, final float[] result, int pixelSize)

Here you can find the source of getHSBfromRGB(final ByteBuffer pixels, final float[] result, int pixelSize)

Description

get HS Bfrom RGB

License

Open Source License

Declaration

public static float[] getHSBfromRGB(final ByteBuffer pixels, final float[] result, int pixelSize) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    public static float[] getHSBfromRGB(final ByteBuffer pixels, final float[] result, int pixelSize) {
        int idx = 0;
        for (int i = result.length / 3; --i >= 0;) {
            float hue;
            final float saturation;
            final float brightness;
            final int r = pixels.get() & 0xFF;
            final int g = pixels.get() & 0xFF;
            final int b = pixels.get() & 0xFF;

            int cmax = (r > g) ? r : g;
            if (b > cmax)
                cmax = b;/*  w w  w .ja v  a 2 s .  c o m*/
            int cmin = (r < g) ? r : g;
            if (b < cmin)
                cmin = b;

            brightness = (cmax) / 255.0f;
            saturation = cmax != 0 ? ((float) (cmax - cmin)) / ((float) cmax) : 0f;

            if (saturation == 0)
                hue = 0;
            else {
                float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
                float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
                float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
                if (r == cmax)
                    hue = bluec - greenc;
                else if (g == cmax)
                    hue = 2.0f + redc - bluec;
                else
                    hue = 4.0f + greenc - redc;
                hue = hue / 6.0f;
                if (hue < 0)
                    hue = hue + 1.0f;
            }
            result[idx++] = hue;
            result[idx++] = saturation;
            result[idx++] = brightness;
            if (pixelSize == 4)
                pixels.get();
        }
        return result;
    }
}

Related

  1. getFloat(ByteBuffer b, int n)
  2. getFloat(ByteBuffer buffer)
  3. getFloat(ByteBuffer floatCalculator, byte[] bytes)
  4. getFromBack(ByteBuffer bb)
  5. getHashBytes(ByteBuffer bb)
  6. getIncreasingByteBuffer(int len)
  7. getIPv4String(ByteBuffer buffer)
  8. getJagexString(ByteBuffer buf)
  9. getJceBufArray(ByteBuffer buffer)