Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6911843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:00:34+00:00 2026-05-27T09:00:34+00:00

I’m trying (In Java) to get the resulting color when I combine 2 colors,

  • 0

I’m trying (In Java) to get the resulting color when I combine 2 colors, the one on top having an alpha transparency. Basically, I’m trying to assign a background color to an image, but I’ve broken it down to each pixel being changed individually. I’ve looked at several articles, including this one, to no avail. Does anybody know how to perform this kind of RGBA/RGB color mixing? My current code uses this png:

PNG Image

And produces this JPG:

JPG Image

This is the function that I’m currently using. The background for the demo images was set to full blue, or an int of 255.

public static void PNGToJPEGConvert(String PNGPath, String NewJPEGPath, int BackColor) throws IOException {
    try {
        BufferedImage bufferedImage = ImageIO.read(new File(PNGPath));
        BufferedImage newImage;
        int R1, G1, B1, R2 = (BackColor & 0xFF0000) >> 16, G2 = (BackColor & 0xFF00) >> 8, B2 = (BackColor & 0xFF), W1, W2, W3, M1, M2, R3, G3, B3;
        float br;
        newImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        for(int x=0; x < bufferedImage.getWidth(); x++) {
            for(int y=0; y < bufferedImage.getHeight(); y++) {
                R1 = BufferedImageGetPixelARGB(bufferedImage, "R", x, y);
                G1 = BufferedImageGetPixelARGB(bufferedImage, "G", x, y);
                B1 = BufferedImageGetPixelARGB(bufferedImage, "B", x, y);
                W1 = Math.min(R1, Math.min(G1, B1));
                W2 = Math.min(R2, Math.min(G2, B2));
                R1 -= W1;
                G1 -= W1;
                B1 -= W1;
                R2 -= W2;
                G2 -= W2;
                M1 = Math.max(R1, Math.max(G1, B1));
                M2 = Math.max(R2, Math.max(G2, B2));
                br = (M1 + M2)/(2*BufferedImageGetPixelARGB(bufferedImage, "A", x, y));
                R3 = (int) ((R1 + R2) * br);
                G3 = (int) ((G1 + G2) * br);
                B3 = (int) ((B1 + B2) * br);
                W3 = (W1 + W2) / 2;
                R3 += W3;
                G3 += W3;
                B3 += W3;
                newImage.setRGB(x, y, RGBValuesToInt(R3, G3, B3));
            }
        }
        try {
            ImageIO.write(newImage, "jpg", new File(NewJPEGPath));
        } catch(Exception e) {

        }
    } catch(Exception e) {

    }
}

Thanks for the help,
-Neil

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:00:35+00:00Added an answer on May 27, 2026 at 9:00 am

    Basically it is

    float masking_factor = mask_value/255.0;
    composed_value = int(
        source_pixel_value * (1 - masking_factor) + 
        overlay_pixel_value * masking_factor
    );
    

    We suppose that all values are 8-bit, that is, 0 to 255, and mask is monochrome. Where mask is completely black, it is fully transparent, and only source pixels are visible. Where mask is completely white, it is fully opaque, and only overlay pixels are visible. Values in between give intermediate opacity.

    But if alpha blending is the only thing you want to implement, there are better ways to do it than working with individual pixels. See Graphics2D and its .setComposite() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.