Author Topic: Color Translation  (Read 2286 times)

How do I translate colors from GuiSliderCtrl to GuiSwatchCtrl color and from GuiSwatchCtrl to the form that color sets use?

guiSwatchCtrl uses RGBA colors (red green blue alpha) in range 0 to 255. I've seen some colorsets that use that same range, however most (including default) seem to use range 0 to 1 (floating point). The conversion method is value / 255.

I'll research guiSliderCtrl in a moment.

guiSliderCtrl is not specifically for colors, you can use it for any type of value. In any case, it's as simple as setting the 'range' member field to '0 255' if you want it to use the same format that guiSwatchCtrl uses.
« Last Edit: April 30, 2012, 06:35:28 AM by Port »

guiSwatchCtrl uses RGBA colors (red green blue alpha) in range 0 to 255. I've seen some colorsets that use that same range, however most (including default) seem to use range 0 to 1 (floating point). The conversion method is value / 255.
Thanks Port, so I just need to do %colorR = getWord(BlahBlah.color, 0) / 255; and so forth?
guiSliderCtrl is not specifically for colors, you can use it for any type of value. In any case, it's as simple as setting the 'range' member field to '0 255' if you want it to use the same format that guiSwatchCtrl uses.
I tried that but whenever I change it to 0 255 it changes it to 0.000000 255.000000.

EDIT: Should I just change the snap to 255 (even though it just looks like one black line after that) and ignore the numbers after the point?
« Last Edit: April 30, 2012, 06:47:06 AM by jes00 »

Thanks Port, so I just need to do %colorR = getWord(BlahBlah.color, 0) / 255; and so forth?

Yes.

I tried that but whenever I change it to 0 255 it changes it to 0.000000 255.000000.

EDIT: Should I just change the snap to 255 (even though it just looks like one black line after that) and ignore the numbers after the point?

range = 0 255

It doesn't matter whether the engine adds stuff like .000000 to the values.
« Last Edit: April 30, 2012, 07:01:00 AM by Port »

Yes.

range = 0 255

It doesn't matter whether the engine adds stuff like .000000 to the values.
range = "0.200000 1.000000"
ticks = "0";
value = "0";
snap = "0";

Sometimes it skips a number or two : /


your problem
Oops, I mean't range = "0.000000 255.000000"

I keep typing in the wrong things today : /

EDIT: I tried translating the colors from a color set to GuiSwatchCtrl but but multiplying them by 255 but it didn't work.
« Last Edit: April 30, 2012, 08:52:25 AM by jes00 »

try multiplying and then using floor on that value

Here is one that I wrote to convert it the other way around

Code: [Select]
function tmbi_coltohex(%col)
{
for(%a=0; %a<3; %a++)
%b[%a] = tmbi_dectohex(getword(%col, %a)*255);
return %b0 @ %b1 @ %b2;
}

function tmbi_dectohex(%x)
{
%a0 = mfloor(%x/16);
%a1 = mfloor(%x - 16*%a0);

for(%i=0; %i<2; %i++)
{
if(%a[%i] == 10)
%a[%i] = "a";
else if(%a[%i] == 11)
%a[%i] = "b";
else if(%a[%i] == 12)
%a[%i] = "c";
else if(%a[%i] == 13)
%a[%i] = "d";
else if(%a[%i] == 14)
%a[%i] = "e";
else if(%a[%i] == 15)
%a[%i] = "f";
}
return %a0 @ %a1;
}

It should be super straightforwardd to reverse it.

Also, it may or may not be useful to note (depending on what you are exactly doing) that gui controls only need to be 0-255 when you are creating the gui object.  After that, you can do gui.setcolor("r g b a"); and those use floats from 0 to 1

You can just have the slider control go 0 to 1 also and you never need to convert to the 0-255 version.

Code: [Select]
function colToHex(%col) {
  for(%word = 0; %word < 3; %word++)
      %return = %return @ toHex(getWord(%col, %word)*255);
  return %return;
}

function toHex(%d) {
  %r = %d % 16;
  if (%d-%r == 0)
    %result = toChar(%r);
  else
    %result = toHex( (%d-%r)/16 ) @ toChar(%r);
  return %result;
}
 
function toChar(%n) {
  %alpha = "0123456789ABCDEF";
  return getSubStr(%alpha,%n,1);
}

That's what I'd use.
« Last Edit: April 30, 2012, 08:28:46 PM by Slicks555 »

This has nothing to do with hexadecimal color codes.

This has nothing to do with hexadecimal color codes.

er wow I have no idea what I was thinking
My thing goes from 0-1 to 0-255 to 0-f

But I think my point about how you can probably keep everything in terms of 0-1 is still valid.
Unless you really want the user to be able to see the color in terms of 0-255, there is no reason to ever do a conversion.

If it is a value between 0 and 1, multiply my 255 and round down.
If it is a hexadecimal value, then you can use my base to base converter to convert it to decimal.
You can find it Here.
It's a very compact script, and does more than just hexadecimal conversions.

BTW, slider always adds decimal places, that's normal.
If you want the slider to snap to whole numbers, try setting the ticks to 254 or 253 and turning snap on.
« Last Edit: May 02, 2012, 11:22:14 AM by Ipquarx »

If you want the slider to snap to whole numbers, try setting the ticks to 254 or 253 and turning snap on.

what

set ticks to 0 and snap to false

If it is a value between 0 and 1, multiply my 255 and round down.
Thank you this works perfectly.

If it is a hexadecimal value, then you can use my base to base converter to convert it to decimal.
You can find it Here.
It's a very compact script, and does more than just hexadecimal conversions.
So I'm to use this method to translate it from the GuiSwatchCtrl to the GuiSlider controls? If so which one do I use?