Anushka Posted April 16, 2020 Posted April 16, 2020 Hello guys, does anyone have any function to convert RGB Colors to HEX? example:255,0,0 to # FF0000 Quote
Jonathan Handojo Posted April 16, 2020 Posted April 16, 2020 That's actually an interesting one, and I'm quite surprised that there's no solution in Google anywhere for AutoLISP. Let me be the first then: ;; JH:RGB->HEX --> Jonathan Handojo ;; Converts an RGB to HEX in the form of a string (defun JH:RGB->HEX (r g b) (apply 'strcat (mapcar '(lambda (x / h) (if (< (strlen (setq h (JH:numhex x))) 2) (strcat "0" h) h)) (list r g b))) ) ;; JH:numhex --> Jonathan Handojo ;; Converts an integer to a hex number in the form of a string (defun JH:numhex (num / hex) (setq hex (mapcar 'cons '(10 11 12 13 14 15) (mapcar 'chr '(65 66 67 68 69 70)))) (apply 'strcat ((lambda (x / l h) (if (zerop x) '("0") (progn (while (/= x 0) (setq l (cons (cond ((cdr (assoc (setq h (rem x 16)) hex))) ((itoa h))) l) x (/ x 16) ) ) l ) ) ) num ) ) ) I'm sure someone has a better approach. _$ (JH:RGB->HEX 45 89 142) "2D598E" 2 Quote
Lee Mac Posted April 16, 2020 Posted April 16, 2020 For example: (defun rgb->hex ( r g b ) (apply 'strcat (mapcar '(lambda ( x ) (setq x (strcat "00" x)) (substr x (1- (strlen x)))) (mapcar 'LM:dec->hex (list r g b)) ) ) ) ;; Decimal to Hexadecimal - Lee Mac ;; Converts a decimal number to hexadecimal string ;; d - [int] 32-bit signed integer ;; Returns: [str] Hexadecimal representation of supplied integer (defun LM:dec->hex ( d ) (if (< d 16) (chr (+ d (if (< d 10) 48 55))) (strcat (LM:dec->hex (/ d 16)) (LM:dec->hex (rem d 16))) ) ) _$ (rgb->hex 255 0 0) "FF0000" 1 Quote
Anushka Posted April 16, 2020 Author Posted April 16, 2020 thank you so much! @Lee Mac and @Jonathan Handojo Quote
Jonathan Handojo Posted April 16, 2020 Posted April 16, 2020 Just now, Anushka said: thank you so much! @Lee Mac and @Jonathan Handojo Glad to help out! @Lee Mac will probably put this function straight into his Colour Conversion Functions site Quote
Anushka Posted April 16, 2020 Author Posted April 16, 2020 My XML asks for an extra value "I think it's opacity" so my red is like this FF0000FF, how can I deal with it ??? Quote
Lee Mac Posted April 16, 2020 Posted April 16, 2020 1 hour ago, Anushka said: My XML asks for an extra value "I think it's opacity" so my red is like this FF0000FF, how can I deal with it ??? Simply concatenate the opacity value? Quote
Jonathan Handojo Posted April 16, 2020 Posted April 16, 2020 49 minutes ago, Lee Mac said: Simply concatenate the opacity value? Opacity of 255? I thought in AutoCAD it's only up to 100 (as in percentage) Quote
Lee Mac Posted April 16, 2020 Posted April 16, 2020 27 minutes ago, Jonathan Handojo said: Opacity of 255? I thought in AutoCAD it's only up to 100 (as in percentage) I imagine that the OP is not using the output to set transparency in AutoCAD, but for some other application which uses RGBA colour coding. 1 Quote
hanhphuc Posted April 17, 2020 Posted April 17, 2020 (edited) 3 hours ago, Lee Mac said: I imagine that the OP is not using the output to set transparency in AutoCAD, but for some other application which uses RGBA colour coding. Thanks from wiki , ARGB max A is FF, but my test in CAD max just reach 7F?? ;;https://en.wikipedia.org/wiki/RGBA_color_model (defun wiki:rgb (lst) ((lambda (i) (apply 'logior (mapcar '(lambda (x) (lsh (fix x) (setq i (- i 8)))) lst) ) ) (* (length lst ) 8) ) ) ;eg ;(wiki:rgb (list 45 89 142)) ;2972046 example : R,G,B,A = 45,89,142,127 (mapcar 'set '(r g b a) '(45 89 142 127)) (LM:dec->hex (wiki:rgb (list a r g b)) ) "7F2D598E" --> AGRB but if A>127, i.e: (128 ~ 255 ) "¾" ??? maybe concatenate either "A+RGB " or "RGB+A" in ARGB case if A=128 (apply 'strcat (mapcar ''((x) (LM:dec->hex x)) (list 128 (wiki:rgb (list r g b))) ) ) Edited April 17, 2020 by hanhphuc syntax color 1 Quote
Lee Mac Posted April 17, 2020 Posted April 17, 2020 Hint: integers in AutoLISP are represented using signed 32-bit integers, unsigned integers aren't available. 1 1 Quote
hanhphuc Posted April 17, 2020 Posted April 17, 2020 (edited) 2 hours ago, Lee Mac said: Hint: integers in AutoLISP are represented using signed 32-bit integers, unsigned integers aren't available. Thanks Lee! +2,147,483,647 to -2,147,483,648 p/s: rgb reminds me UTF-8 time flies! 4.18? cheers guru Edited April 17, 2020 by hanhphuc 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.