I've upvoted Johannes' answer because he's right about that.

* A few comments have been raised that my original answer was not correct. It worked if alpha values were inverted from the normal. By definition, however, this won't work in most cases. I've therefore updated the formula below to be correct for the normal case. This ends up being equal to @hkurabko's answer below *

A more specific answer, however, incorporates the alpha value into the actual colour result based on an opaque background colour (or 'matte' as it's referred to).

There is an algorithm for this (from this wikipedia link):

  • Normalise the RGBA values so that they're all between 0 and 1 - just divide each value by 255 to do this. We'll call the result Source.
  • Normalise also the matte colour (black, white whatever). We'll call the result BGColor Note - if the background colour is also transparent, then you'll have to recurse the process for that first (again, choosing a matte) to get the source RGB for this operation.
  • Now, the conversion is defined as (in complete psuedo code here!):

    Source => Target = (BGColor + Source) =
    Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
    Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
    Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)
    

To get the final 0-255 values for Target you simply multiply all the normalised values back up by 255, making sure you cap at 255 if any of the combined values exceed 1.0 (this is over-exposure and there are more complex algorithms dealing with this that involve whole-image processing etc.).

EDIT: In your question you said you want a white background - in that case just fix BGColor to 255,255,255.

Answer from Andras Zoltan on Stack Overflow
🌐
W3Schools
w3schools.com › colors › colors_rgb.asp
Colors RGB and RGBA
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255), and the other two (green and blue) are set to 0.
🌐
Tdekoning
tdekoning.github.io › rgba-converter
Rgba to hex/rgb converter
To use this converter, enter the rgba(R, G, B, A) or hsla(H, S, L, A) string into the first input field and the hexadecimal color of the background into the second input field. It is also possible to enter an rgb or hsl definition for the background.
People also ask

How long does RGBA to RGB conversion take?
Conversion time is usually seconds for single images under 20 MB in browser-based tools and faster on modern desktops; larger files or batches can take longer depending on CPU and disk speed. Server-side services may add upload and download time, so total time varies with connection speed and file size.
🌐
convertfiles.com
convertfiles.com › convert › image › rgba-to-rgb
Online RGBA to RGB Converter - Convert RGBA to RGB Online Fast
Will I lose quality converting RGBA to RGB?
Converting by simply removing alpha and exporting to a lossless RGB format (like PNG or TIFF) does not change color channels and preserves quality, but compositing onto a background can alter edge appearance. If you export to a lossy format (JPEG/WebP), you will introduce compression artifacts depending on the quality setting.
🌐
convertfiles.com
convertfiles.com › convert › image › rgba-to-rgb
Online RGBA to RGB Converter - Convert RGBA to RGB Online Fast
Can I convert multiple RGBA files to RGB at once?
Yes—most desktop image editors and many online converters support batch conversion so you can apply the same compositing and export settings to many files. For large batches, use CLI tools or desktop apps to avoid browser upload limits and improve processing speed.
🌐
convertfiles.com
convertfiles.com › convert › image › rgba-to-rgb
Online RGBA to RGB Converter - Convert RGBA to RGB Online Fast
🌐
Borderleft
borderleft.com › toolbox › rgba
RGBA to RGB converter
Convert RGBA color like rgba(0,0,255,0.5) to RGB hex value that incorporates the alpha channel.
🌐
My Fix Guide
myfixguide.com › color-converter
Color Converter - RGB, HEX, HSL, ARGB, RGBA
This color converter tool can convert colors between 5 formats: RGB, HEX, HSL, ARGB, and RGBA, including HEX to RGB, HEX to RGBA, RGB to HEX, RGBA to HEX, HEX to HSL, RGB to RGBA, RGBA to RGB.
Top answer
1 of 10
106

I've upvoted Johannes' answer because he's right about that.

* A few comments have been raised that my original answer was not correct. It worked if alpha values were inverted from the normal. By definition, however, this won't work in most cases. I've therefore updated the formula below to be correct for the normal case. This ends up being equal to @hkurabko's answer below *

A more specific answer, however, incorporates the alpha value into the actual colour result based on an opaque background colour (or 'matte' as it's referred to).

There is an algorithm for this (from this wikipedia link):

  • Normalise the RGBA values so that they're all between 0 and 1 - just divide each value by 255 to do this. We'll call the result Source.
  • Normalise also the matte colour (black, white whatever). We'll call the result BGColor Note - if the background colour is also transparent, then you'll have to recurse the process for that first (again, choosing a matte) to get the source RGB for this operation.
  • Now, the conversion is defined as (in complete psuedo code here!):

    Source => Target = (BGColor + Source) =
    Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
    Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
    Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)
    

To get the final 0-255 values for Target you simply multiply all the normalised values back up by 255, making sure you cap at 255 if any of the combined values exceed 1.0 (this is over-exposure and there are more complex algorithms dealing with this that involve whole-image processing etc.).

EDIT: In your question you said you want a white background - in that case just fix BGColor to 255,255,255.

2 of 10
48

hm... regarding to

http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending

solution provided by Andras Zoltan should be slightly changed to:

Source => Target = (BGColor + Source) =
Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)

This changed version works fine for me, because in prev. version rgba(0,0,0,0) with matte rgb(ff,ff,ff) will be changed to rgb(0,0,0).

🌐
Convertio
convertio.co › rgb-rgba
RGB to RGBA (Online & Free) — Convertio
Best way to convert your RGB to RGBA file in seconds. 100% free, secure and easy to use! Convertio — advanced online tool that solving any problems with any files.
Find elsewhere
🌐
W3Schools
w3schools.com › css › css_colors_rgb.asp
CSS RGB and RGBA Colors
To display white, set all color parameters to 255, like this: rgb(255, 255, 255). ... RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity for a color.
🌐
ConvertFiles
convertfiles.com › convert › image › rgba-to-rgb
Online RGBA to RGB Converter - Convert RGBA to RGB Online Fast
Use our online RGBA to RGB converter to quickly and easily convert RGBA images to RGB format. Convert RGBA to RGB online with no downloads required.
🌐
RGBA Color Picker
rgbacolorpicker.com
RGBA Color Picker
Pick Colors from our interactive and fast RGBA (RGB + Alpha) Color Picker.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Reference › Values › color_value › rgb
rgb() - CSS | MDN
The rgb() functional notation expresses a color in the sRGB color space according to its red, green, and blue components. An optional alpha component represents the color's transparency. ... /* Absolute values */ rgb(255 255 255) rgb(255 255 255 / 50%) /* Relative values */ rgb(from green r ...
Top answer
1 of 5
5

The fastest was would be to use a library that implements the conversion for you rather than writing it yourself. Which platform[s] are you targeting?

If you insist on writing it yourself for some reason, write a simple and correct version first. Use that. If the performance is inadequate, then you can think about optimizing it. In general, this sort of conversion is best done using vector permutes, but the exact optimal sequence varies depending on the target architecture.

2 of 5
5

How tricky do you want it? You could set it up to copy a 4-byte word at a time, which might be a bit faster on some 32-bit systems:

void fast_unpack(char* rgba, const char* rgb, const int count) {
    if(count==0)
        return;
    for(int i=count; --i; rgba+=4, rgb+=3) {
        *(uint32_t*)(void*)rgba = *(const uint32_t*)(const void*)rgb;
    }
    for(int j=0; j<3; ++j) {
        rgba[j] = rgb[j];
    }
}

The extra case on the end is to deal with the fact that the rgb array is missing a byte. You could also make it a bit faster using aligned moves and SSE instructions, working in multiples of 4 pixels at a time. If you're feeling really ambitious, you can try even more horribly obfuscated things like prefetching a cache line into the FP registers, for example, then blitting it across to the other image all at once. Of course the mileage you get out of these optimizations is going to be highly dependent on the specific system configuration you are targetting, and I would be really skeptical that there is much benefit at all to doing any of this instead of the simple thing.

And my simple experiments confirm that this is indeed a little bit faster, at least on my x86 machine. Here is a benchmark:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>

void fast_unpack(char* rgba, const char* rgb, const int count) {
    if(count==0)
        return;
    for(int i=count; --i; rgba+=4, rgb+=3) {
        *(uint32_t*)(void*)rgba = *(const uint32_t*)(const void*)rgb;
    }
    for(int j=0; j<3; ++j) {
        rgba[j] = rgb[j];
    }
}

void simple_unpack(char* rgba, const char* rgb, const int count) {
    for(int i=0; i<count; ++i) {
        for(int j=0; j<3; ++j) {
            rgba[j] = rgb[j];
        }
        rgba += 4;
        rgb  += 3;
    }
}

int main() {
    const int count = 512*512;
    const int N = 10000;

    char* src = (char*)malloc(count * 3);
    char* dst = (char*)malloc(count * 4);

    clock_t c0, c1;    
    double t;
    printf("Image size = %d bytes\n", count);
    printf("Number of iterations = %d\n", N);

    printf("Testing simple unpack....");
    c0 = clock();
    for(int i=0; i<N; ++i) {
        simple_unpack(dst, src, count);
    }
    c1 = clock();
    printf("Done\n");
    t = (double)(c1 - c0) / (double)CLOCKS_PER_SEC;
    printf("Elapsed time: %lf\nAverage time: %lf\n", t, t/N);


    printf("Testing tricky unpack....");
    c0 = clock();
    for(int i=0; i<N; ++i) {
        fast_unpack(dst, src, count);
    }
    c1 = clock();
    printf("Done\n");
    t = (double)(c1 - c0) / (double)CLOCKS_PER_SEC;
    printf("Elapsed time: %lf\nAverage time: %lf\n", t, t/N);

    return 0;
}

And here are the results (compiled with g++ -O3):

Image size = 262144 bytes

Number of iterations = 10000

Testing simple unpack....Done

Elapsed time: 3.830000

Average time: 0.000383

Testing tricky unpack....Done

Elapsed time: 2.390000

Average time: 0.000239

So, maybe about 40% faster on a good day.

🌐
GitHub
gist.github.com › mzechmeister › 943ce68c9c84d662cea1ecd42bc3064b
rgba2rgb · GitHub
Save mzechmeister/943ce68c9c84d662cea1ecd42bc3064b to your computer and use it in GitHub Desktop. Download ZIP · rgba2rgb · Raw · gistfile1.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
RGB Color Picker
rgbcolorpicker.com
RGB Color Picker
A fast and simple RGB color picker. Drag the pointer to change the color and copy the RGB or Hex value in one click.
🌐
RunComfy
runcomfy.com › comfyui-nodes › comfyui-reactor-node › image-rgba2rgb
Convert RGBA to RGB 🌌 ReActor
January 6, 2025 - Converts RGBA images to RGB, removing alpha channel for compatibility and visual consistency.
🌐
Free File Converter
freefileconvert.com › home › rgba converter › rgba to rgb
Convert RGBA to RGB (Free & Online) - FreeFileConvert
Free online tool to convert RGBA (Raw RGBA Image) files to RGB (Silicon Graphics RGB). No download required. Fast, secure, and 100% free.
🌐
OpenReplay
openreplay.com › tools › rgba-to-hex
RGBA to HEX Converter
To convert from 8-digit hex to RGBA, split the hex into RGB components and convert the last two digits to a decimal between 0 and 1 for the alpha value.
🌐
Marcodiiga
marcodiiga.github.io › rgba-to-rgb-conversion
RGBA to RGB conversion
December 1, 2016 - How to convert a RGBA color likergba(0, 0, 0, 0.86)to a RGB hex value that takes the alpha component into account?
🌐
Wikipedia
en.wikipedia.org › wiki › RGBA_color_model
RGBA color model - Wikipedia
July 23, 2024 - RGBA stands for red green blue alpha. While it is sometimes described as a color space, it is actually a three-channel RGB color model supplemented with a fourth alpha channel. Alpha indicates how opaque each pixel is and allows an image to be combined over others using alpha compositing, with transparent areas and anti-aliasing of the edges of opaque regions.