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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:47:25+00:00 2026-05-27T21:47:25+00:00

Possible Duplicate: given a background color, how to get a foreground color that make

  • 0

Possible Duplicate:
given a background color, how to get a foreground color that make it readable on that background color?

I wonder, if there is any algorithm to determine the optimal fontcolor for readability by its background color.

For example:
I create an icon with dynamic text and color. If the color is kinda dark, I want it to set the font color to white and if its fairly bright, I want it to be in black (or maybe even grayish).

  public DynamicIcon( String iconText, Color backgroundColor )
  {
    this.iconText = iconText;
    this.backgroundColor = backgroundColor;

    this.fontColor = determineFontColor( backgroundColor );
  }

  //This is what I need (Pseudocode):
  private fontColor determineFontColor( Color backgroundColor )
  {
    if( backgroundColor == bright )
      return Color.BLACK;
    if( backgroundColor == dark )
      return Color.WHITE;
    //If possible:
    if( backgroundColor == somethingInBetween )
      return Color.GRAYISH;
  }

Unfortunatly I didnt find any algorithm like that, eventhough I am kinda sure, that it already exists. Anyone has any ideas?

thanks,
ymene

  • 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-27T21:47:26+00:00Added an answer on May 27, 2026 at 9:47 pm

    We had to do something like that on our system: based on the background, we colour the font black or white. The solution we found is not perfect and chooses the wrong colour on some rare occasions, but we are very satisfied with it.

    This is what we did:

    int red = 0;
    int green = 0;
    int blue = 0;
    
    if ( backgroundColor.red + backgroundColor.green + backgroundColor.blue < 383 ) {
        red = 255;
        green = 255;
        blue = 255;
    }
    

    And then we use red, green and blue values to create a new Color object.

    The magical number 383 is the result of ( 255 + 255 + 255 ) / 2

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

Sidebar

Related Questions

Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Possible Duplicate: Get all files from VSS for a given date? I need to
Possible Duplicate: Run a code in given time interval I have a method that
Possible Duplicate: How do I determine if a given date is Nth weekday of
Possible Duplicate: Get first day of week in PHP? Given a timestamp I need
Possible Duplicate: RegEx to make sure that the string contains at least one lower
Possible Duplicate: Change custom color for Rectangle.Fill or Grid.Background I'm trying to dynamically set
Possible Duplicate: Calculate number of days between two given dates Is there a way
Possible Duplicate: How to determine the date one day prior to a given date
Possible Duplicate: Size of struct with a single element Given any type A and

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.