Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 17 Oct 2018, 16:48
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Converting 12 digits number to 6 digits number and reverse?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 3 [36 Posts]   Goto page: 1, 2, 3 Next
Author Message
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Mon 26 Sep 2016, 16:27    Post subject:  Converting 12 digits number to 6 digits number and reverse?  

Hi.

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

etc.pp.

Roxfiler is using a 12 digit number like:
Code:
#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) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 16:52    Post subject:  

@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.
Back to top
View user's profile Send private message 
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Mon 26 Sep 2016, 17:13    Post subject:  

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) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 17:20    Post subject:  

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.

Quote:

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.
Back to top
View user's profile Send private message 
Sailor Enceladus

Joined: 22 Feb 2016
Posts: 1546

PostPosted: Mon 26 Sep 2016, 17:49    Post subject:  

Is this how it works? I have no idea Laughing

Code:
#!/bin/sh
printf "0x%X\n" `echo $[(($1 & 0xFF)<<8) + ((($1 >> 8) & 0xFF)<<24) + (($1 >> 16)<<36)]`

Quote:
# ./script 0x332211
0x330022001100
# ./script 0x8000FF
0x80000000FF00
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 919

PostPosted: Mon 26 Sep 2016, 18:04    Post subject:  

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

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

then-
pick first two of each group

Code:
# color_12='#440044004400'
#  cut -c 1-3,6,7,10,11 <<< "$color_12"
#444444


Cheers,
s
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 18:04    Post subject:  

@Sailor Enceladus

errrr..no, that ain't gonna cut it.
48 bit number in, some bit shifting and 48 bit number out.
Back to top
View user's profile Send private message 
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Mon 26 Sep 2016, 18:05    Post subject:  

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

Code:
#!/bin/sh
printf "0x%x\n" `echo $[(($1 % 0x100)<<8) + ((($1 >> 8) % 0x100)<<24) + (($1 >> 16)<<36)]`

Quote:
# ./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.

Quote:
...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) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 18:07    Post subject:  

@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?
Back to top
View user's profile Send private message 
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Mon 26 Sep 2016, 18:10    Post subject:  

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) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 18:14    Post subject:  

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.
Back to top
View user's profile Send private message 
Sailor Enceladus

Joined: 22 Feb 2016
Posts: 1546

PostPosted: Mon 26 Sep 2016, 18:15    Post subject:  

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.
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 18:22    Post subject:  

@LazY Puppy

Have a look at the strtol and strtoul functions in C.
Back to top
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1665

PostPosted: Mon 26 Sep 2016, 18:26    Post subject:  

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:
# 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!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
Pete


Joined: 02 Mar 2014
Posts: 675

PostPosted: Mon 26 Sep 2016, 18:32    Post subject:  

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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 3 [36 Posts]   Goto page: 1, 2, 3 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0574s ][ Queries: 12 (0.0060s) ][ GZIP on ]