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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:34:58+00:00 2026-06-07T12:34:58+00:00

I am looking to sort a list by color distance. I am using the

  • 0

I am looking to sort a list by “color distance.” I am using the following function to calculate the distance between two colors:

double colorDistance(Color c1, Color c2) {
    double rmean = (c1.getRed() + c2.getRed()) / 2;
    int r = c1.getRed() - c2.getRed();
    int g = c1.getGreen() - c2.getGreen();
    int b = c1.getBlue() - c2.getBlue();
    double weightR = 2 + rmean / 256;
    double weightG = 4.0;
    double weightB = 2 + (255 - rmean) / 256;
    return Math.sqrt(weightR * r * r + weightG * g * g + weightB * b * b);
}

So, as you can see, I am using the RGB values to calculate “distance” between two colors. This function works fantastically for what I need to calculate as I don’t require exact precision. The problem is, I need to be able to tell, not only distance, but whether c1 comes before or after c2 so that I can create a sorted list based on color distance.

In case that isn’t clear enough, an example might help. The color distance between pure black and pure white will be somewhere in the vicinity of 256. In its current state, this function will return the same ~256 for colorDistance(black, white) and colorDistance(white, black). How could I make it so that colorDistance(black, white) returns 256 and colorDistance(white, black) returns -256?

I’m not averse to using HSV/HSL if that is an option, but I am far more familiar with RGB. If a function could be provided for calculating positive or negative color distance with HSV/HSL then bring it on!

Or even better yet, another simple function that will take two colors and simply decide whether c1 comes before or after c2!

  • 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-07T12:35:00+00:00Added an answer on June 7, 2026 at 12:35 pm

    You can multiply the answer by negative 1 if:

    int pixel = (red & 0xFF) << 24 |
                  (blue & 0xFF) << 16 |
                  (green & 0xFF) <<  8 |
                  (255 & 0xFF); // 255 is alpha value
    

    is less than the other color’s pixel.

    Edit: I left alpha value in case you want to use it in the future. If not, then you can take that part out and reduce every shift by 8. Like so:

    int pixel = (red & 0xFF) << 16 |
                (blue & 0xFF) << 8 |
                (green & 0xFF);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>
I'm looking on how to sort a list effectively using weighted values. Each item
I'm looking for a way to sort a list of object (of any type
Im looking to group and sort a Generic List<>. I have a list of
I'm looking to write a comparator to sort my items in a list. For
I am looking at this example: List<Product> products = Product. GetSampleProducts() ; products.Sort( (first,
I am looking to sort an XML structure using XSLT using xsltproc: The xml
I am looking for a function that will take a list of options in
I'm looking for a fast way to sort a list in reverse order in
I'm looking for some sample code that will sort the list items in an

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.