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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:37:26+00:00 2026-06-17T13:37:26+00:00

I wanted to write a function which could check if color is close to

  • 0

I wanted to write a function which could check if color is close to a background color.

For this i make use of the HSL color scheme, let me explain that a bit; HSL colors are defined as Hue Saturation and lightness. In short Hue tells you which color of the rainbow is used and its in the range 0 to 360. And its on the Hue that i would like check.

So Saturation how strong the color is, like pure or mixed with grey
or Lightness which speaks for itself are not compared. I only want to check for Hue.
At first i wrote a Near function like this:

Private boolean Near(int background, int mycolor, int difference)
{
  if(math.abs( background - mycolor)<difference){return true;}else{return false}
}

Later i realized this is wrong. Because HSL is like the image below you see in there the colors are as a circle, so starting at red 0 and it is red again at 360.
So a Hue value of 358 and 4 are close together, the above function wouldn’t reflect that.

(Saturation goes to the center 0..100
lightens 0..100 is like going up or down
and Hue is from 0 to 360 degrees around. )

enter image description here

I could rewrite a function with a large if then construction, so that for example if the background where red 5 and the allowed difference is 20 then mycolor would be in range if it is < 5+20 or > (360-(5-20)) .. so making a special construction if the Hue difference would be crossing the 0 or 360 limits.

Well that would work, but then i wondered might it be possible to replace such “if then” construction with a modulo calculation in a single line ?, it keeps me wondering still
As modulo computations can be used to check within a border and if its over the border of 360 it would be red again close to zero,

well i think so, such line might also contain some AND or OR and subtractions or ABS functions..might it be possible to write it in one comparison line ?

  • 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-17T13:37:27+00:00Added an answer on June 17, 2026 at 1:37 pm

    What you need to do is ensure that the values you are comparing are as close as they can be. For instance, if background = 358, and mycolor = 4, then they could be made closer by subtracting 360 from background, which doesn’t change the effective value. They need to be made closer as long as the difference > 180.

    if (mycolor - background > 180) mycolor -= 360;
    if (background - mycolor > 180) background -= 360;
    

    With these transformations, your original logic should then be correct (the if is unnecessary):

    return Math.Abs(background - mycolor) < difference;
    

    Edit: I have found a simpler expression that seems to be correct.

    return Math.Abs(background - mycolor) % (361 - difference) < difference;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write my own hash function. If I wanted to just make
today I wanted to write a function which will deploy my blog page (developed
I wanted to write a function that'll be cross platform (win32 & linux), and
I'm playing around with beginner Haskell, and I wanted to write an average function.
Just wanted to write some recursion but can't check if the child is in
I'm using oh-my-zsh and wanted to write a small plugin which would executes command
I want to write a rename function to replace String names (which represent hierarchical
Possible Duplicate: How do I change the background color with Javascript? I have this
I wanted to write a query to delete from x table and y table
I wanted to write something basic in assembly under Windows. I'm using NASM, but

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.