Java Color to RGB String toRGB565(Color c)

Here you can find the source of toRGB565(Color c)

Description

Returns a color in the RGB565 format based on a Color instance.

License

Open Source License

Parameter

Parameter Description
c the color to convert into a RGB565 color.

Return

a color in the RGB565 format based on the specified Color.

Declaration

public static short toRGB565(Color c) 

Method Source Code

//package com.java2s;
/**/*from w w  w.ja v  a2 s . co m*/
 * Copyright (c) 2001-2016 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://robocode.sourceforge.net/license/epl-v10.html
 */

import java.awt.*;

public class Main {
    /**
     * Returns a color in the RGB565 format based on a Color instance.
     *
     * @param c the color to convert into a RGB565 color.
     * @return a color in the RGB565 format based on the specified Color.
     */
    public static short toRGB565(Color c) {
        if (c == null) {
            return 0;
        }
        short rgb = (short) (((c.getRed() & 0xf8) << 8) | ((c.getGreen() & 0xfc) << 3) | (c.getBlue() >> 3));

        // 0 is reserved for null -> set green (has highest resolution) to 1
        if (rgb == 0) {
            return 0x20;
        }
        // If the color actually was 0x20 then set it to 0x40 (to the nearest green)
        if (rgb == 0x20) {
            return 0x40;
        }
        return rgb;
    }
}

Related

  1. colourToString(java.awt.Color c)
  2. toRGB(Color c)
  3. toRGB(Color color)
  4. toRGB(ColorSpace colorSpace, float... components)
  5. toRGB(int red, int green, int blue)
  6. toRGBA(Color c)
  7. toRGBFunctionCall(Color color)