Windows [SOLVED]Q:Display color values

Z

zendraw

Guest
Hi,
does some1 know how can i get to display the hex value of a color? Do i have to create such a script myself?
 
Z

zendraw

Guest
so i went ahead and made my own script, dunno what script were u talking about in gmlscripts?
i tested it and its accurate.
Code:
///scr_color_value(color);

// B/G/R

var b=colour_get_blue(argument0);
var g=colour_get_green(argument0);
var r=colour_get_red(argument0);

var b10=floor(b/16);
var b1=b-(b10*16);

var g10=floor(g/16);
var g1=g-(g10*16);

var r10=floor(r/16);
var r1=r-(r10*16);

var c;

c[0]='0';
c[1]='1';
c[2]='2';
c[3]='3';
c[4]='4';
c[5]='5';
c[6]='6';
c[7]='7';
c[8]='8';
c[9]='9';
c[10]='A';
c[11]='B';
c[12]='C';
c[13]='D';
c[14]='E';
c[15]='F';

return '$'+string(c[b10])+string(c[b1])+string(c[g10])+string(c[g1])+string(c[r10])+string(c[r1]);
 
Z

zendraw

Guest
its kinda hard to figure out this script, if u care to explain...
 
You put in a decimal value and you get a hexadecimal value out of it. :)
But there is no need to use it now when you already got it working
 
Z

zendraw

Guest
i mean i dont get his code, the script`s function is obvious, but i want to know whats he doing.
 
D

dannyjenn

Guest
its kinda hard to figure out this script, if u care to explain...
That particular script isn't all that intuitive in my opinion (it sort of works its way backwards and assembles the string from back to front), but here it is in pseudocode:
Code:
If the input is not 0, we don't yet know what its output is. Otherwise, the output is "00".
We then define a string with all the hex digits in ascending order, such that '0' is in position 0 and 'F' is in position 15. (Note: Every position maps directly to its corresponding hex value.)
Afterwards, we need to split our input number into individual bytes. So to do that, we set up a loop.{
    We first pull out the least signifiant byte, by masking the number with $FF.
    Then we figure out the high nibble (i.e. the leftmost 4 bits) and we convert that value into a letter by looking it up in our string.
    Then we figure out the low nibble (i.e. the rightmost 4 bits) and we also convert it to a letter the same way
    Then we insert these two nibbles to the front of our output.
    Lastly, we discard the least significant byte of our input and move everything else over by a byte, and we loop until there are no longer any bytes in our input.
}
Then we return the output.
It's basically using a string (instead of an array) to look up the hex values of the nibbles, and the rest is just a matter of looping in order to convert the entire number. (If you're only working with color codes, you don't really need to loop, since all color codes have 3 bytes (or 4 bytes if you include the alpha).


There are various other ways of doing this sort of thing as well. I've found that the most straightforward way is to just store all 255 possible hex values in a ds_map, and look them up. (This way is faster (or at least it was faster back in the days before the YoYoCompiler came out), since you're working with bytes rather than nibbles so there's less calculations involved. But it does take up more RAM, since you need to store 255 2-character strings in a ds_map rather than 16 1-character strings in an array.)
 
Last edited by a moderator:
Z

zendraw

Guest
You can set b11 (and the other colors) as b&15. You can set b10 as b>>4.
you mean this
Code:
var b10=b>>4;
var b1=b&15;

var g10=g>>4;
var g1=g&15;

var r10=r>>4;
var r1=r&15;
yeah i shuld get to use binary more. thanks. altho i get b>>4 i dont get how g&15 gives me the correct resault? isnt & equivilent to and? atleast thats what the manual says.
 
Z

zendraw

Guest
ooh i get it, its like mod but only in certain cases like 7, 15, 31.. etc where all the bits are 1 or in other words, just before you get to the division number 8, 16, 32 etc. pretty neat.
 
^ I think it's compare the all numbers bitwise. Let's say you have 22 which is 10110 binary and 10 which is 1010 binary.
So it's starts from the right.
0 & 0 = 0
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
1 & nothing = nothing

So the result is 0010 which is 2 in decimal.
Please correct me if I'm wrong.
 
Z

zendraw

Guest
yes and it can be used as mod when you compare it to a 7, 15, 31 etc. number basically the divisible ones -1.
 
Top