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 8881153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:19+00:00 2026-06-14T20:17:19+00:00

I have hex rgb color and black-white mask. It’s two integer arrays: mColors =

  • 0

I have hex rgb color and black-white mask. It’s two integer arrays:

mColors = new int[] {
                 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
                 0xFFFFFF00, 0xFFFF0000
             };
mColorsMask = new int[] {
                     0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF,
                     0xFFFFFFFF, 0xFF000000
                 };

I need to convert my color to black value depending on contrast. Contrast is integer value in a range from 0 to 255:
enter image description here

With white all is fine, I make byte addition:

int newHexColor = (contrast << 16) | (contrast << 8) | contrast | mColors[i];
newColorsArray[i] = mode;

How to convert it to black?

  • 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-06-14T20:17:20+00:00Added an answer on June 14, 2026 at 8:17 pm

    You might look into using the HSB color space. It seems much more suited to what you’re trying to do. In particular, you see those angles that end up black in your “what i want” image? Those correspond to “hues” at 60, 180, and 300 degrees (1.0/6, 3.0/6, and 5.0/6 in Java). The white corresponds to 0, 120, and 240 degrees (0, 1.0/3, and 2.0/3 in Java) — and not coincidentally, the colors at those angles are primary colors (that is, two of the three RGB components are zero).

    What you’d do is find the difference between your color’s hue and the nearest primary color. (Should be less than 1/6.) Scale it up (multiplying by 6 should do it), to give you a value between 0 and 1.0. That will give you an “impurity” value, which is basically the deviation from the nearest primary color. Of course, that number subtracted from 1.0 gives you the “purity”, or the closeness to a primary color.

    You can create a greyscale color based on the impurity or purity by using the respective value as the R, G, and B, with an alpha of 1.0f.

    public Color getMaskColor(Color c) {
        float[] hsv = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
        float hue = hsv[0];
    
        // 0, 1/3, and 2/3 are the primary colors.  Find the closest one to c,
        // by rounding c to the nearest third.
        float nearestPrimaryHue = Math.round(hue * 3.0f) / 3.0f;
    
        // difference between hue and nearestPrimaryHue <= 1/6
        // Multiply by 6 to get a value between 0 and 1.0
        float impurity = Math.abs(hue - nearestPrimaryHue) * 6.0f;
        float purity = 1.0f - impurity;
    
        // return a greyscale color based on the "purity"
        // (for #FF0000, would return white)
        // using impurity would return black instead
        return new Color(purity, purity, purity, 1.0f);
    }
    

    You could either use a color component of the returned color as the “contrast” value, or change the function so that it returns the “purity” or “impurity” as needed.

    Note, the math gets wonky with greyscale colors. (The way Java calculates HSB, pure greys are just reds (hue=0) with no tint (saturation=0). The only component that changes is the brightness.) But since your color wheel doesn’t have greyscale colors…

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

Sidebar

Related Questions

I have a hex color, e.g. #F4F8FB (or rgb(244, 248, 251) ) that I
I have an array of RGB hex colors. I would like to find a
let's say that I have a HEX color #a08040. How can I determine in
Hey everyone, just a quick thing, I have the hex to integer working, but
I have created a variable using System.Windows.Media.Color. I can display the Hex value from
I have a few colors (rgb/hex codes) that I would like to be available
I have a list of colors in HEX format: String[] validcolors = new String[]
Let's suppose I have an RGB string (format: #<2 hex digits><2 hex digits><2 hex
I need to compute the difference between two hex color values so the output
I have a hex value (0x0020004E0000 ... which is the base address to a

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.