advertisement

Article:
  Creating a Color Meter Using Cocoa
Subject:   How to display the Colervalues in Hex oder %?
Date:   2005-01-23 08:22:00
From:   Humunculus
hi,
i like your articels very much. Thx for them.
I'm a very beginner in ObjC...


What i'm thinking about is, how can i transform this very uncomfortable floatnumbers in hex or percentage-description, like you would see it in most apps like these or just in the color-picker?


Normally you would display RGB-Color-Numbers like this:
255 - 255 -255 wich is the counterpart to
#ff ff ff and means the color white.
So you get values for each color between 0 and 255 or 00 and ff.
If you multiply R*G*B (256*256*256) you get 16.7mill colors, which is the colorspace a screen is able to display and only this values makes sence.
So i was very surprised to see the values the method's ask for, a float from 0 to 1.
But the space between 0 and 1 in float is infinite.
How you would transform a floatnumber with unknown digits into hex or percent?
I expected to find a class in the documentation which will do it for me, but i didn't.


Normaly i would try to make a modulo, but 0.12345n % 255 is 0.
So i tried to find a method in NSNumbers to make out of 0.12345n to 12345n. But i didn't find one.
Other way would be to transform the float into a string and remove the 0. at the beginning and transform again into an int.
...and so on.


What is the reason for the asked values of 0 to 1??
I never saw this before.