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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:19:04+00:00 2026-06-05T11:19:04+00:00

I have the numbers 1000 to 9999. I would like to have a function

  • 0

I have the numbers 1000 to 9999. I would like to have a function that gets an unique color in rgb for each number. The colors needs to be the same for each number every time the method is launched. Also the colors need to be very different when adding a number. So number 1 can be dark red, 2 light green, etc.

Hopefully some one knows how to do this! 🙂

This is what I got so far, but the colors are almost identical, thats not what I want!

for ($i = 1000; $i < 2099; $i++) {
  $rgb = getRGB(dechex($i));
  echo '<div style="width: 800px; height: 30px; margin-bottom: 10px; background-color: ' . rgb2html($rgb['red'], $rgb['green'], $rgb['blue']) . ';"></div>';
}

function getRGB($psHexColorString) {
  $aColors = array();
  if ($psHexColorString{0} == '#') {
    $psHexColorString = substr($psHexColorString, 1);
  }
  $aColors['red'] = @hexdec($psHexColorString{0} . $psHexColorString{1});
  $aColors['green'] = @hexdec($psHexColorString{2} . $psHexColorString{3});
  $aColors['blue'] = @hexdec($psHexColorString{4} . $psHexColorString{5});
  return $aColors;
}

function rgb2html($r, $g = -1, $b = -1) {
  if (is_array($r) && sizeof($r) == 3)
    list($r, $g, $b) = $r;

  $r = intval($r);
  $g = intval($g);
  $b = intval($b);

  $r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
  $g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
  $b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));

  $color = (strlen($r) < 2 ? '0' : '') . $r;
  $color .= (strlen($g) < 2 ? '0' : '') . $g;
  $color .= (strlen($b) < 2 ? '0' : '') . $b;
  return '#' . $color;
}
  • 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-05T11:19:05+00:00Added an answer on June 5, 2026 at 11:19 am

    You can use the php function “dechex” to convert integer to Hexa RGB :

    http://php.net/manual/en/function.dechex.php

    In this page there is this function :

    function toColor($n) {
        return("#".substr("000000".dechex($n),-6));
    }
    

    Not tested but can be help.

    EDIT after comment : you can add some pseudo-randomiser in the function:

    function toColor($n) {
        $n = crc32($n);
        $n &= 0xffffffff;
        return("#".substr("000000".dechex($n),-6));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generator (numbers) and a value (number). I would like to iterate
I have 15,000 numbers ranging between 1000-8000. Each number is assigned to one of
I have a varchar(1000) column declared as field that contains all numbers, as shown
I have a table containing numbers, range between 1-1000 they are unique, but I
I would like to compare every possible combination of numbers up to 1000 (or
I have 1000 large numbers, randomly distributed in range 37231 to 56661. I am
I have 1M numbers:N[], and 1 single number n, now I want to find
I have one collection numbers with 200000 document object with {number: i} i =
I have the following numbers : 1,2,3,4,5,6,7,8,9,10,11,12... I have a number n, say n=2
Possible Duplicate: Math - mapping numbers I have value x that can be from

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.