-1

Is there any way in which, given a number between 0 and 16581375 (255*255*255) to an RGB value- such as 0 at (0,0,0) and 16581375 as (255,255,255)?

Obviously, in addition to this, there are factors such as he ordering of RGB values, and different factors in themselves.

The reason I am asking this question is because in order to get greater resolution of a mathematical structure I need more colours (rather than just 255 shades of grey).

So, is there any way in which I can do this?

user2592835
  • 1,547
  • 5
  • 18
  • 26
  • Do you mean 16777216 (256 * 256 * 256) ? – falsetru Sep 01 '14 at 13:54
  • whats your question now? do you want to make a random `rgb` value ? – Mazdak Sep 01 '14 at 13:54
  • In answer to the above question, no. Considering pygame only goes up to (255,255,255)? – user2592835 Sep 01 '14 at 13:57
  • And to jonrsharpe: I am not sure.Is there any way in which the system could be fixed- so the combination goes from low to high? – user2592835 Sep 01 '14 at 13:58
  • @user2592835 You need to explain how to get RGB from a single value. It is clear that `value = R * G * B` but how do you divide them up? – Cory Kramer Sep 01 '14 at 14:01
  • ...then you wouldn't be able to represent all colours, only those for which `r < g < b`. (And the examples should have been e.g. `(125, 192, 250)`, `(200, 120, 250)`, `(240, 125, 200)`, as `300` isn't in range!) – jonrsharpe Sep 01 '14 at 14:02

1 Answers1

0

Not very sexy, but...

... given your sample value:

>>> n = 16581375
>>> h = "{:06X}".format(n)
>>> (int(h[0:2],16),int(h[2:4],16),int(h[4:6],16))
(253, 2, 255)

The min:

>>> n = 0
>>> h = "{:06X}".format(n)
>>> (int(h[0:2],16),int(h[2:4],16),int(h[4:6],16))
(0, 0, 0)

The max:

>>> n = 256*256*256-1
>>> h = "{:06X}".format(n)
>>> (int(h[0:2],16),int(h[2:4],16),int(h[4:6],16))
(255, 255, 255)

For the mathematically inclined:

>>> n = 16581375
>>> (n//(256*256), n//256%256, n%256)
(253, 2, 255)

And for the logically inclined:

>>> n = 16581375
>>> (n >> 16, (n >> 8) & 0xFF, n & 0xFF)
(253, 2, 255)
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • What if `n` is 0? You'd better to use something like `format(n, '06x')` to make sure the 0 padding. – falsetru Sep 01 '14 at 13:59
  • @falsetru Using the right `format()` fixed that :/... I was troubled by 16581375 not returning (255,255,255). But the actual max is 256*256*256-1 == 16777215, not the value given by the OP. – Sylvain Leroux Sep 01 '14 at 14:03