I try to store integer (real numbers) values into pixel data. The only way my api can store pixel data are RGB Colors. The idea behind it is, to store a large amount of vertices into the vram, rather than into the ram and compare them. A RBG Color is created by rgb(r, g, b), where r, g and b can have 256 different values (0 - 255).

So i could theoretically store 256^3 values. The integer could be greater than that, but i dont need that high numbers.

My goal is to store for example:
integer 1 = rgb(1, 0, 0);
integer 2 = rgb(0, 1, 0);

My math understanding has slightly decreased the years after graduating, but im aware, that if im looping first through red, then green and at the end blue, i could only use 256*3 numbers.

Therefore my question is: how could i convert (or split) any real number below 256^3 into 3 variables (r, g, b).

No special languague syntax is required, but im happy with c or java syntax

  • $\begingroup$ You need a structure to hold (at least) 24 bits and bit shift operations. c++ example (but you can use integers and pure c) stackoverflow.com/questions/2682725/… $\endgroup$ – z100 Feb 1 '16 at 15:20

The accepted answer I saw is incorrect. Again, we write $$C = 256^2 R + 256 G + B$$ for positive integers $R,G,B < 256$. We clearly see that $B\equiv C \mod{256}$, or in pseudocode B = C % 256. That part was correct.

Now we just subtract $B$ from $C$ and repeat the process.

\begin{align} & C - B = 256^2 R + 256 G \\ \implies & \frac{C-B}{256} = 256 R + G \\ \implies & G \equiv \frac{C-B}{256} \mod{256} \end{align}

And again:

\begin{align} & \frac{C-B}{256} - G = 256 R \\ \implies & R = \frac{C-B}{256^2} - \frac{G}{256} \,. \end{align}

In conclusion, we have

B = C % 256
G = ((C-B)/256) % 256
R = ((C-B)/256**2) - G/256

We could also do this algorithmically, not that it's efficient or useful:

# Assuming that C is a positive integer less than or equal to 0xffffff
x = C
# Repeatedly subtract 1 from x = C until x is a multiple of 0x10000
while x % 0x10000 != 0:
    x -= 1
# We have our red component
R = int(x / 0x10000)
# Repeat for the green component
y = c - x
while y % 0x100 != 0:
    y -= 1
# We have our green component
G = int(y / 0x100)
# And finally our blue component
B = C - x - y # == C - (x + y)
| cite | improve this answer | |

I think what you're looking for is the same as hex valued colors. These are of the form:

$$ X_6X_5X_4X_3X_2X_1 $$

where $X_2X_1$ is a hex valued number representing the blue value, $X_4X_3$ is a hex valued number representing the green value, and $X_6X_5$ is a hex valued number representing the red value. All you have to do is represent your integer as:

C = 256^2* R + 256* G + B;

then you have that

R = C/(256^2);

G = (C/256) % 256;

B = C%256;

| cite | improve this answer | |
  • $\begingroup$ thank you would be an understatement in this case :) $\endgroup$ – TheDomis4 Feb 1 '16 at 19:22
  • $\begingroup$ A new user has posted a correction to your second formula (in the form of an "answer"). I'd encourage the use of parentheses as further improvement. $\endgroup$ – hardmath Jul 4 '18 at 4:20
  • $\begingroup$ @hardmath Corrected it now! Sorry about that. $\endgroup$ – David Kleiman Jul 12 '18 at 21:02
  • $\begingroup$ Found this because I had the same question. Sorry, but your equations for $R$ and $G$ are just wrong. The quantities $C/256$ and $C/256^2$ are rarely integers. I'll try to post an answer shortly $\endgroup$ – terrygarcia Sep 3 '19 at 17:30
  • $\begingroup$ @terrygarcia It's been a while since I wrote this answer, but I believe that I intended for the division to be integer division $\endgroup$ – David Kleiman Sep 10 '19 at 12:23

There is a typo in your formula for the green component. Corrected:

R = C/(256^2);

G = (C/256) % 256;

B = C%256;

| cite | improve this answer | |
  • $\begingroup$ This is more a Comment on the Answer posted by David Kleiman than it is a standalone Answer to the original Question. As you gain reputation, you will quickly get the privilege of commenting on the posts of others (50 points). Your corrections suffer from the same lack of parentheses as the uncorrected formulas. $\endgroup$ – hardmath Jul 4 '18 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.