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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:49:13+00:00 2026-06-03T15:49:13+00:00

Is it possible to check if a certain hexadecimal color is closer to FFF

  • 0

Is it possible to check if a certain hexadecimal color is closer to FFF or 000 based on a defined ‘center-value’?

I want to check if a color lies closer to #FFF or #000 based on #888.
So if I check for #EFEFEF it should return #FFF and if I try #878787 it should return #000.

How can this be achieved? I’m not sure what to search for on Google…

Thanks in advance

  • 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-03T15:49:14+00:00Added an answer on June 3, 2026 at 3:49 pm

    The easiest way to solve your problem is to calculate the distance between colors using their greyscale values (there are other ways, but this is simple). So something like:

    // returns a distance between two colors by comparing each component
    // using average of the RGB components, eg. a grayscale value
    function color_distance($a, $b)
    {
        $decA = hexdec(substr($a, 1));
        $decB = hexdec(substr($a, 1));
        $avgA = (($decA & 0xFF) + (($decA >> 8) & 0xFF) + (($decA >> 16) & 0xFF)) / 3;
        $avgB = (($decB & 0xFF) + (($decB >> 8) & 0xFF) + (($decB >> 16) & 0xFF)) / 3;
        return abs($avgA - $avgB);
    }
    
    // I am going to leave the naming of the function to you ;)
    // How this works is that it'll return $minColor if $color is closer to $refColorMin
    // and $maxColor if $color is closer to $refColorMax
    // all colors should be passed in format #RRGGBB
    function foo($color, $refColorMin, $refColorMax, $minColor, $maxColor)
    {
        $distMin = color_distance($color, $refColorMin);
        $distMax = color_distance($color, $refColorMax);
        return ($distMin < $distMax) ? $minColor : $maxColor;
    }
    
    // Example usage to answer your original question:
    $colorA = foo('#EFEFEF', '#888888', '#FFFFFF', '#000000', '#FFFFFF');
    $colorA = foo('#898989', '#888888', '#FFFFFF', '#000000', '#FFFFFF');
    // Check the values
    var_dump($colorA, $colorB);
    

    The output is:

    string(7) "#FFFFFF"
    string(7) "#000000"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to check the value of a certain key in a PHP
Possible Duplicate: Efficient way to test string for certain words I want to check
Is it possible to leave a ContextMenuStrip open after a selection/check of certain items?
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: How to check JRE version prior to launch? Many people want to
Possible Duplicates: Attribute Value Selection in SimpleXML SimpleXML: Selecting Elements Which Have A Certain
I want to check if a generic variable is of a certain type but
Possible Duplicate: C# Execute function at specific time I want to run certain function
Possible Duplicate: Java page re-direct I want to go to a certain page the
Possible Duplicate: Check that an email address is valid on iOS What is the

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.