Author Topic: hexadecimal color conversion  (Read 730 times)

0 Members and 1 Guest are viewing this topic.

XXL66

  • Newt
  • Posts: 99
hexadecimal color conversion
« on: October 28, 2015, 04:40:18 AM »
hello,

i have colors in an XML file in hexadecimal format

f.e.

"0xFF00A0"
"0x000000"
"0x20FF00"

is it possible to translate these into an autocad color ?

thanks !


XXL66

  • Newt
  • Posts: 99
Re: hexadecimal color conversion
« Reply #1 on: October 28, 2015, 04:57:38 AM »
Code: [Select]
(defun hex2true ($ / )
  (LM:RGB->True (HexadecimalToInteger (substr $ 3 2)) (HexadecimalToInteger (substr $ 5 2)) (HexadecimalToInteger (substr $ 7 2)))
)

(defun LM:RGB->True ( r g b )
    (logior (lsh (fix r) 16) (lsh (fix g) 8) (fix b))
)

(defun HexadecimalToInteger (strHexadecimal / intInteger intAscii)
 (setq intInteger 0)
 (foreach intAscii (vl-string->list (strcase strHexadecimal))
  (setq intInteger (+ (lsh intInteger 4)
                     
                      (- intAscii
                       (if (<= intAscii 57) 48 55)
                      )
                   )
  )
 )
)

i think i'm close, but lines that are purple become blue, something must be wrong...

i think i'm close, but lines that are purple become blue, something must be wrong...

Lee Mac

  • Seagull
  • Posts: 12285
  • London, England
Re: hexadecimal color conversion
« Reply #2 on: October 28, 2015, 05:50:04 AM »
I see nothing wrong with the code, and in my testing the following will create a line with colour 255,0,160 as expected:

Code: [Select]
_$ (entmake (list '(0 . "LINE") '(10 0 0 0) '(11 1 0 0) (cons 420 (hex2true "0xFF00A0"))))
((0 . "LINE") (10 0 0 0) (11 1 0 0) (420 . 16711840))

XXL66

  • Newt
  • Posts: 99
Re: hexadecimal color conversion
« Reply #3 on: October 28, 2015, 06:07:59 AM »
hi thx

indeed, but i use polylines. It seems bcad adds the 62 dxf group code also with my entmake (header + vertices)...  i think 62 is used instead of dxf 420...
any idea what i'm doing wrong ?

ty !

roy_043

  • Water Moccasin
  • Posts: 1731
  • BricsCAD 18
Re: hexadecimal color conversion
« Reply #4 on: October 28, 2015, 07:42:50 AM »
The presence of gc 62 is to be expected for elements with a true color.
Perhaps you are also supplying gc 62 in your code? It is best to leave it out.
If both gc 62 and 420 are present in the entmake list the last entry takes precedence.

True color rejected:
Code - Auto/Visual Lisp: [Select]
  1.   '(
  2.     (0 . "LWPOLYLINE")
  3.     (8 . "0")
  4.     (420 . 16711840)
  5.     (62 . 16711840)
  6.     ...
  7.    
  8.   )
  9. )

True color accepted:
Code - Auto/Visual Lisp: [Select]
  1.   '(
  2.     (0 . "LWPOLYLINE")
  3.     (8 . "0")
  4.     (62 . 16711840)
  5.     (420 . 16711840)
  6.     ...
  7.    
  8.   )
  9. )

XXL66

  • Newt
  • Posts: 99
Re: hexadecimal color conversion
« Reply #5 on: October 28, 2015, 08:26:16 AM »
hi roy,

I do not supply 62. i didn't notice that 62 was also added to a 'simple' line.
I tried changing into BGR instead of RGB also but it still isn't correct.
It seems there is a bug in the software i import the data from, the color on the screen does not match the output. So it is someone elses problem now ;-)

thanks you guys !

XXL66

  • Newt
  • Posts: 99
Re: hexadecimal color conversion
« Reply #6 on: October 28, 2015, 10:37:56 AM »
is there a color code for BYLAYER for DXF group code 420 ?
when i select an object it shows 62 192 192 in true color, however when adding this value this doesn't result in bylayer.
i have to use 256 in code dxf group 62 ?

Lee Mac

  • Seagull
  • Posts: 12285
  • London, England
Re: hexadecimal color conversion
« Reply #7 on: October 28, 2015, 01:31:03 PM »
i have to use 256 in code dxf group 62 ?

I would suggest this, yes.

XXL66

  • Newt
  • Posts: 99
Re: hexadecimal color conversion
« Reply #8 on: October 28, 2015, 02:28:31 PM »
ok thx !