Converting 12 digits number to 6 digits number and reverse?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

Converting 12 digits number to 6 digits number and reverse?

#1 Post by LazY Puppy »

Hi.

Usually to define a color in gtkdialog one would use something like that:

Code: Select all

#DB09AC
etc.pp.

Roxfiler is using a 12 digit number like:

Code: Select all

#440044004400
How to convert such numbers to the opposite (12 to 6 and 6 to 12)?

Want to do this from within a bash script!

Thanks.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#2 Post by Pete »

@LaZy Puppy

Converting is the easy part but you have resolution issues to take care of.
In the first notation, each colour (RED, GREEN, BLUE) has only 8 bits, 00-FF in hex or 0-255 in decimal, making up 8bits X 3 colours i.e 24 bit colour.

In the second notation, it has 16 bits per colour 0000-FFFF (also hex) or 0-65535 in decimal, therefore its 3 words (not bytes in this case) X 3 colours giving 48 bit colour.

So when converting from 24 bit to 48 bit, you will be restricted in colours and have to pad the missing bits with zeros and vice versa, you will have to leave out certain colours as 24 bits will simply not have the gamut of 48 bit colour.

So say you have a colour of #4467AA997744, there is no way you will be able to represent that accurately in 24 bit as there are simply too many colours or hues.
So when doing the conversion, best you can do is approximations, which may be OK for your application, but keep in mind that with certain colour combinations, it will look very odd and nothing like the original.
It will be like trying to show a HD video in SD, not only is there not enough pixels, the colours simply don't exist.
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#3 Post by LazY Puppy »

Hi.

Thanks for the reply.

Wasn't that helpful to my issue/purpose.

Though, I don't care about 24 bit colors and/or 48 bit colors.

I just want to convert the values like #DB09AC to #14354860 (the blue part) like it can be done in ycalc - and reverse.

Since this seems to be a one-click mathematical function in ycalc, I just thought about a script or a binary (or probably some C code to compile) that will do the job, so that I'm able to process this from within a bash script.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#4 Post by Pete »

LazY Puppy wrote:Hi.

Thanks for the reply.

Wasn't that helpful to my issue/purpose.

Though, I don't care about 24 bit colors and/or 48 bit colors.
Whether you care or not is not the issue, I was simply trying to inform you of the possible pitfalls when converting from one colour space to another or you may well be disappointed with the final results.
I just want to convert the values like #DB09AC to #14354860....
I think you are missing the point.
The calculation/convertion is the easy part but it does not mean you are going to get the correct colours.
You may well get some colours that will overlap and look right, but not all combinations will work and since there are 16,777,216 combinations (including full white and black) for 24 bit colour, good luck testing them all.
Sailor Enceladus
Posts: 1543
Joined: Mon 22 Feb 2016, 19:43

#5 Post by Sailor Enceladus »

Is this how it works? I have no idea :lol:

Code: Select all

#!/bin/sh
printf "0x%X\n" `echo $[(($1 & 0xFF)<<8) + ((($1 >> 8) & 0xFF)<<24) + (($1 >> 16)<<36)]`
# ./script 0x332211
0x330022001100
# ./script 0x8000FF
0x80000000FF00
seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

Perhaps this will do.. if.....

#RRGGBB = (8 bits each)
#RRRRGGGGBBBB = (16 bits each)

then-
pick first two of each group

Code: Select all

# color_12='#440044004400'
#  cut -c 1-3,6,7,10,11 <<< "$color_12"
#444444
Cheers,
s
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#7 Post by Pete »

@Sailor Enceladus

errrr..no, that ain't gonna cut it.
48 bit number in, some bit shifting and 48 bit number out.
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#8 Post by LazY Puppy »

Sailor Enceladus wrote:Is this how it works? I have no idea :lol:

Code: Select all

#!/bin/sh
printf "0x%x\n" `echo $[(($1 % 0x100)<<8) + ((($1 >> 8) % 0x100)<<24) + (($1 >> 16)<<36)]`
# ./script 0x332211
0x330022001100
# ./script 0x8000ff
0x80000000ff00
No, it doesn't.

It can't handle alphabetical characters and also it can't handle numbers starting with a 0 (zero) in front.
...does not mean you are going to get the correct colours
I don't care about the correct colours. If there will be something really weird I can change its value manually afterwards.

It's more a general purpose - not focused too much to every single color possible.

Just want to convert those values like it is done in a calculator.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#9 Post by Pete »

@seaside

Problem comes in when there is colour info in the least significant bytes.

For example 440044004400 ==> 444444 is OK but what about

4497448844FF ? it's a valid 48 bit colour, but how to represent it in 24 bit?
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#10 Post by LazY Puppy »

Wouldn't it be possible to get the sourcecode of ycalc, just to grab those two functions that are converting hex to dec and dec to hex used to build/compile a binary just performing those two functions?
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#11 Post by Pete »

LazY Puppy wrote: I don't care about the correct colours. If there will be something really weird I can change its value manually afterwards.

It's more a general purpose - not focused too much to every single color possible.

Just want to convert those values like it is done in a calculator.
If all you want is the conversion with no regard for colour, then seasides method ain't bad as it's discarding only the lower order.
Sailor Enceladus
Posts: 1543
Joined: Mon 22 Feb 2016, 19:43

#12 Post by Sailor Enceladus »

LazY Puppy wrote:It can't handle alphabetical characters and also it can't handle numbers starting with a 0 (zero) in front.
It handles alphabetic characters (look at the example), I was asking if that was how the conversion actually works. Based on Pete's post, I'm guessing it isn't though. Sure, pad some 0s to the front if you want when needed.
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#13 Post by Pete »

@LazY Puppy

Have a look at the strtol and strtoul functions in C.
User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#14 Post by SFR »

LazY Puppy wrote:Wouldn't it be possible to get the sourcecode of ycalc, just to grab those two functions that are converting hex to dec and dec to hex used to build/compile a binary just performing those two functions?
So, all you want is just hex<->dec conversion?
Well, it's simple as that:

Code: Select all

# printf '%d\n' 0xdb09ac
14354860
# 
# printf '%x\n' 14354860
db09ac
# 
However it has nothing to do with the way ROX stores some colors, e.g. #440044004400 from your first post is not decimal, but 48bit hexadecimal number and its 24bit approximation is #444444.

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#15 Post by Pete »

Sailor Enceladus wrote:Based on Pete's post, I'm guessing it isn't though. Sure, pad some 0s to the front if you want when needed.
Your script works for 24 bit ==> 48 bit as the lower order bytes will always be zeroes.(nice simple script BTW).
What LP wants to do is other way round, 48 bits ==> 24 bits.

Since there is no way that the colour spaces match plus he mentioned that colour fidelity is not important, he might as well use seasides method of approximation as SFR mentioned.
Of course the data in the lower order bytes will be discarded, but that is what approximation is.

BTW this method is often used in the video world with 8 bit and 10 bit video.
8 bit ==> 10 bit, pad with zeroes
10 bit ==> 8 bit, shift towards MSB and drop the LSBs.
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#16 Post by LazY Puppy »

However it has nothing to do with the way ROX stores some colors, e.g. #440044004400 from your first post is not decimal, but 48bit hexadecimal number and its 24bit approximation is #444444.
Ok, I see.

So I made a false assumption at the colours stored by rox filer. Found out right now:

- entering 440044004400 into ycalc and clicking the hex button returns a:

- - math library error - base conversion

Though, there must be a solution, since rox filer is using a gui to select colours (the colours to display files). In an entry field the chosen colour is presented like:

- #db09ac

But when it is stored into /root/.config/rox.sourceforge.net/ROX-Filer/Options it has changed its format to a 16 digit number.

So, the main direction to convert (and I'm mainly focused at) is conversion of 8 digit number to 12 digit number.
Last edited by LazY Puppy on Tue 27 Sep 2016, 00:00, edited 1 time in total.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
User avatar
Pete
Posts: 660
Joined: Sun 02 Mar 2014, 18:36

#17 Post by Pete »

Remember that each "digit" is hexadecimal with values of 0 - F (4 bits), so a 16 digit number in hex is 16 x 4 or 64 bits, are you sure that you don't mean 12 digits? which would be 48 bits.

Or maybe they are just padding to always show 16 digits due to some other reason but since the 4 most significant "digits" will always be zeros, it is still only 48 bits and the extra zeros in front are only for alignment purposes.
User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

Re: Converting 12 digits number to 6 digits number and reverse?

#18 Post by greengeek »

LazY Puppy wrote:Roxfiler is using a 12 digit number like:

Code: Select all

#440044004400
Where do you find this number? Which file does rox-filer store this in please?
Sailor Enceladus
Posts: 1543
Joined: Mon 22 Feb 2016, 19:43

Re: Converting 12 digits number to 6 digits number and reverse?

#19 Post by Sailor Enceladus »

greengeek wrote:
LazY Puppy wrote:Roxfiler is using a 12 digit number like:

Code: Select all

#440044004400
Where do you find this number? Which file does rox-filer store this in please?
/root/.config/rox.sourceforge.net/ROX-Filer/Options is one of them ;)
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#20 Post by LazY Puppy »

Pete wrote:are you sure that you don't mean 12 digits? which would be 48 bits.
Yes, of course. Like the title said.

Typo fixed. :lol:
greengeek wrote:Where do you find this number? Which file does rox-filer store this in please?
File /root/.config/rox.sourceforge.net/ROX-Filer/Options is the one where the colours to display files are stored at.

When booting my T.O.P.L.E.S.S. Puppies it analyzes the current set gtktheme and if there is a dark theme used the colours will change to make file names etc. more readable for human eyes.

Dark green and dark blue is just too dark on a dark background like in the murrina-black gtk theme.

Currently I'm editing the parameters in the config file manually using the 12 digit number, though I want to include a function to choose colours by the use of the Xdialog --colorsel widget/function which uses the 6 digit format as it is done in rox filer.

So, I'm in the need to convert that 6 digit number into that 12 digit number used by rox filer.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
Post Reply