Skip to content

RGB to PixelIt-Integer #346

Answered by foorschtbar
supermonchen asked this question in Q&A
Discussion options

You must be logged in to vote

You search for RGB565, PixelIt use this representation for images.

function rgbHexToRgb565(hex) {
    // Extract individual RGB components from hex
    const red = parseInt(hex.substring(1, 3), 16);
    const green = parseInt(hex.substring(3, 5), 16);
    const blue = parseInt(hex.substring(5, 7), 16);

    // Convert RGB to RGB565
    const r5 = (red >> 3) & 0x1F;
    const g6 = (green >> 2) & 0x3F;
    const b5 = (blue >> 3) & 0x1F;

    // Combine components into RGB565 integer
    const rgb565 = (r5 << 11) | (g6 << 5) | b5;

    return rgb565;
}

// Example usage
const hexColor = "#FF0000";
const rgb565Value = rgbHexToRgb565(hexColor);
console.log(rgb565Value);

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@supermonchen
Comment options

@supermonchen
Comment options

Comment options

You must be logged in to vote
4 replies
@supermonchen
Comment options

@foorschtbar
Comment options

Answer selected by supermonchen
@supermonchen
Comment options

@rknotter
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants