You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by chitgoks <ch...@gmail.com> on 2017/03/13 10:27:04 UTC

converting hex to PDColor

hi again

a little assistance regarding converting hex to PDColor.

please take this example #ff8000

and this is my code

String colorStr = "#ff8000";
java.awt.Color rgb = new java.awt.Color(
            Integer.valueOf(colorStr.substring(1, 3), 16),
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16))

PDColor pdcolor = new PDColor(new float[] { rgb.getRed() / 255,
rgb.getGreen() / 255, rgb.getBlue() / 255}, PDDeviceRGB.INSTANCE);

the result is pink-ish (the wrong color), instead of orange-ish (the
correct color).

Re: converting hex to PDColor

Posted by chitgoks <ch...@gmail.com>.
that worked. thank you very much



On Mon, Mar 13, 2017 at 8:37 PM, chitgoks <ch...@gmail.com> wrote:

> thank you all for thebresponse
>
> i will try this out and confirm
>
>
> On Mon, 13 Mar 2017 at 8:26 PM Lachezar Dobrev <l....@gmail.com> wrote:
>
>>   Hmm...
>>
>>   1. java.awt.Color.decode(colorStr);
>>   2. You're using integer division "rgb.getRed()/255" will yield 0 or
>> 1, which is then cast to float. Use "getRed()/255f" to get a float
>> result.
>>
>>   Your integer division code will only yield a red colour with
>> #FF8000, which I suspect gets superimposed on a white background (with
>> alpha, dithering, blurring, whatever) and ends up being pinkish.
>>
>>   Also the Color step is totally unnecessary:
>>
>>   int c = Integer.parseInt(colorStr.substring(1), 16);
>>   float r = ((c & 0xFF0000) >> 16) / 255f;
>>   float g = ((c & 0x00FF00) >>  8) / 255f;
>>   float b = ((c & 0x0000FF) >>  0) / 255f; // The ">> 0" can be omitted
>>   PDColor pdc = new PDColor( new float[] { r, g, b },
>> PDDeviceRGB.INSTANCE);
>>
>> 2017-03-13 12:27 GMT+02:00 chitgoks <ch...@gmail.com>:
>> > hi again
>> >
>> > a little assistance regarding converting hex to PDColor.
>> >
>> > please take this example #ff8000
>> >
>> > and this is my code
>> >
>> > String colorStr = "#ff8000";
>> > java.awt.Color rgb = new java.awt.Color(
>> >             Integer.valueOf(colorStr.substring(1, 3), 16),
>> >             Integer.valueOf(colorStr.substring(3, 5), 16),
>> >             Integer.valueOf(colorStr.substring(5, 7), 16))
>> >
>> > PDColor pdcolor = new PDColor(new float[] { rgb.getRed() / 255,
>> > rgb.getGreen() / 255, rgb.getBlue() / 255}, PDDeviceRGB.INSTANCE);
>> >
>> > the result is pink-ish (the wrong color), instead of orange-ish (the
>> > correct color).
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>> --
> Website/Java Games: http://www.chitgoks.com
> My Blogs:
> http://tech.chitgoks.com
> http://wuhtevah.chitgoks.com
> http://disneyusa.blogspot.com
>

Re: converting hex to PDColor

Posted by chitgoks <ch...@gmail.com>.
thank you all for thebresponse

i will try this out and confirm


On Mon, 13 Mar 2017 at 8:26 PM Lachezar Dobrev <l....@gmail.com> wrote:

>   Hmm...
>
>   1. java.awt.Color.decode(colorStr);
>   2. You're using integer division "rgb.getRed()/255" will yield 0 or
> 1, which is then cast to float. Use "getRed()/255f" to get a float
> result.
>
>   Your integer division code will only yield a red colour with
> #FF8000, which I suspect gets superimposed on a white background (with
> alpha, dithering, blurring, whatever) and ends up being pinkish.
>
>   Also the Color step is totally unnecessary:
>
>   int c = Integer.parseInt(colorStr.substring(1), 16);
>   float r = ((c & 0xFF0000) >> 16) / 255f;
>   float g = ((c & 0x00FF00) >>  8) / 255f;
>   float b = ((c & 0x0000FF) >>  0) / 255f; // The ">> 0" can be omitted
>   PDColor pdc = new PDColor( new float[] { r, g, b },
> PDDeviceRGB.INSTANCE);
>
> 2017-03-13 12:27 GMT+02:00 chitgoks <ch...@gmail.com>:
> > hi again
> >
> > a little assistance regarding converting hex to PDColor.
> >
> > please take this example #ff8000
> >
> > and this is my code
> >
> > String colorStr = "#ff8000";
> > java.awt.Color rgb = new java.awt.Color(
> >             Integer.valueOf(colorStr.substring(1, 3), 16),
> >             Integer.valueOf(colorStr.substring(3, 5), 16),
> >             Integer.valueOf(colorStr.substring(5, 7), 16))
> >
> > PDColor pdcolor = new PDColor(new float[] { rgb.getRed() / 255,
> > rgb.getGreen() / 255, rgb.getBlue() / 255}, PDDeviceRGB.INSTANCE);
> >
> > the result is pink-ish (the wrong color), instead of orange-ish (the
> > correct color).
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
> --
Website/Java Games: http://www.chitgoks.com
My Blogs:
http://tech.chitgoks.com
http://wuhtevah.chitgoks.com
http://disneyusa.blogspot.com

Re: converting hex to PDColor

Posted by Lachezar Dobrev <l....@gmail.com>.
  Hmm...

  1. java.awt.Color.decode(colorStr);
  2. You're using integer division "rgb.getRed()/255" will yield 0 or
1, which is then cast to float. Use "getRed()/255f" to get a float
result.

  Your integer division code will only yield a red colour with
#FF8000, which I suspect gets superimposed on a white background (with
alpha, dithering, blurring, whatever) and ends up being pinkish.

  Also the Color step is totally unnecessary:

  int c = Integer.parseInt(colorStr.substring(1), 16);
  float r = ((c & 0xFF0000) >> 16) / 255f;
  float g = ((c & 0x00FF00) >>  8) / 255f;
  float b = ((c & 0x0000FF) >>  0) / 255f; // The ">> 0" can be omitted
  PDColor pdc = new PDColor( new float[] { r, g, b }, PDDeviceRGB.INSTANCE);

2017-03-13 12:27 GMT+02:00 chitgoks <ch...@gmail.com>:
> hi again
>
> a little assistance regarding converting hex to PDColor.
>
> please take this example #ff8000
>
> and this is my code
>
> String colorStr = "#ff8000";
> java.awt.Color rgb = new java.awt.Color(
>             Integer.valueOf(colorStr.substring(1, 3), 16),
>             Integer.valueOf(colorStr.substring(3, 5), 16),
>             Integer.valueOf(colorStr.substring(5, 7), 16))
>
> PDColor pdcolor = new PDColor(new float[] { rgb.getRed() / 255,
> rgb.getGreen() / 255, rgb.getBlue() / 255}, PDDeviceRGB.INSTANCE);
>
> the result is pink-ish (the wrong color), instead of orange-ish (the
> correct color).

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: converting hex to PDColor

Posted by Andreas Lehmkühler <an...@lehmi.de>.
> chitgoks <ch...@gmail.com> hat am 13. März 2017 um 11:27 geschrieben:
> 
> 
> hi again
> 
> a little assistance regarding converting hex to PDColor.
> 
> please take this example #ff8000
> 
> and this is my code
> 
> String colorStr = "#ff8000";
> java.awt.Color rgb = new java.awt.Color(
>             Integer.valueOf(colorStr.substring(1, 3), 16),
>             Integer.valueOf(colorStr.substring(3, 5), 16),
>             Integer.valueOf(colorStr.substring(5, 7), 16))
> 
> PDColor pdcolor = new PDColor(new float[] { rgb.getRed() / 255,
> rgb.getGreen() / 255, rgb.getBlue() / 255}, PDDeviceRGB.INSTANCE);
You can omit the PDColor step as those int values are already the values you are looking for
red = Integer.valueOf(colorStr.substring(1, 3), 16) and so on.

> 
> the result is pink-ish (the wrong color), instead of orange-ish (the
> correct color).
Where do you see that wrong color? In the resulting PDF? If the latter, please share the doc with us.

BR
Andreas

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org