如何在Scheme中将十进制转换为十六进制?(需要在GIMP中将RGB转换为十六进制)

如何在Scheme中将十进制转换为十六进制?(需要在GIMP中将RGB转换为十六进制),scheme,gimp,script-fu,Scheme,Gimp,Script Fu,如何在Scheme中将十进制转换为十六进制 需要在GIMP中将RGB转换为十六进制以用于JSON: (set! imgcolor (car (gimp-color-picker image newDraw 1 1 TRUE TRUE 1))) 赋体。现在的结果是RGB格式的:(255)在Barak Itkin编写的pallete-export.scm中查找答案 ; For all the operations below, this is the order of respectable di

如何在Scheme中将十进制转换为十六进制

需要在GIMP中将RGB转换为十六进制以用于JSON:

(set! imgcolor (car (gimp-color-picker image newDraw 1 1 TRUE TRUE 1)))

赋体。现在的结果是RGB格式的:(255)

在Barak Itkin编写的pallete-export.scm中查找答案

; For all the operations below, this is the order of respectable digits:
(define conversion-digits (list "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
                                "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"
                "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
                "w" "x" "y" "z"))


; Converts a decimal number to another base. The returned number is a string
(define (convert-decimal-to-base num base)
  (if (< num base) 
    (list-ref conversion-digits num) 
    (let loop ((val num)
               (order (inexact->exact (truncate (/ (log num) 
                                                   (log base)))))
               (result ""))
      (let* ((power (expt base order))
             (digit (quotient val power)))
        (if (zero? order)
          (string-append result (list-ref conversion-digits digit)) 
          (loop (- val (* digit power))
                (pred order)
                (string-append result (list-ref conversion-digits digit))))))))


; Convert a color to a hexadecimal string
; '(255 255 255) => "#ffffff"
(define (color-rgb-to-hexa-decimal color)
  (string-append "#"
                 (pre-pad-number
          (convert-decimal-to-base (car color) 16) 2 "0")
                 (pre-pad-number
          (convert-decimal-to-base (cadr color) 16) 2 "0")
                 (pre-pad-number
          (convert-decimal-to-base (caddr color) 16) 2 "0")
                 )
  )

; Присваеваем HEX переменной 
(set! imgcolorHEX (color-rgb-to-hexa-decimal imgcolor)) 
;对于以下所有操作,这是值得尊敬的数字顺序:
(定义转换数字(列出“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”)
“a”“b”“c”“d”“e”“f”“g”“h”“i”“j”“k”
“l”“m”“n”“o”“p”“q”“r”“s”“t”“u”“v”
“w”“x”“y”“z”))
; 将十进制数转换为另一个基数。返回的数字是一个字符串
(定义(将十进制转换为基数)
(如果(精确(截断(/(对数)
(对数基((())))
(结果“))
(let*((电源(出口基本订单))
(数字(商值幂)))
(如果(零订单)
(字符串附加结果(列表参考转换数字))
(环路(-val(*数字功率))
(预订单)
(字符串附加结果(列表参考转换位数(()()()())))
; 将颜色转换为十六进制字符串
; '(255)=>“#ffffff”
(定义(颜色rgb到十六进制颜色)
(字符串附加“#”
(预焊盘编号
(将十进制转换为基数(汽车颜色)16)2“0”)
(预焊盘编号
(将十进制转换为基数(cadr颜色)16)2“0”)
(预焊盘编号
(将十进制转换为基数(caddr颜色)16)2“0”)
)
)
; Паааааааа六角
(设置!imgcolorHEX(颜色rgb到十六进制imgcolor))

在Barak Itkin编写的pallete-export.scm中找到答案

; For all the operations below, this is the order of respectable digits:
(define conversion-digits (list "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
                                "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"
                "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
                "w" "x" "y" "z"))


; Converts a decimal number to another base. The returned number is a string
(define (convert-decimal-to-base num base)
  (if (< num base) 
    (list-ref conversion-digits num) 
    (let loop ((val num)
               (order (inexact->exact (truncate (/ (log num) 
                                                   (log base)))))
               (result ""))
      (let* ((power (expt base order))
             (digit (quotient val power)))
        (if (zero? order)
          (string-append result (list-ref conversion-digits digit)) 
          (loop (- val (* digit power))
                (pred order)
                (string-append result (list-ref conversion-digits digit))))))))


; Convert a color to a hexadecimal string
; '(255 255 255) => "#ffffff"
(define (color-rgb-to-hexa-decimal color)
  (string-append "#"
                 (pre-pad-number
          (convert-decimal-to-base (car color) 16) 2 "0")
                 (pre-pad-number
          (convert-decimal-to-base (cadr color) 16) 2 "0")
                 (pre-pad-number
          (convert-decimal-to-base (caddr color) 16) 2 "0")
                 )
  )

; Присваеваем HEX переменной 
(set! imgcolorHEX (color-rgb-to-hexa-decimal imgcolor)) 
;对于以下所有操作,这是值得尊敬的数字顺序:
(定义转换数字(列出“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”)
“a”“b”“c”“d”“e”“f”“g”“h”“i”“j”“k”
“l”“m”“n”“o”“p”“q”“r”“s”“t”“u”“v”
“w”“x”“y”“z”))
; 将十进制数转换为另一个基数。返回的数字是一个字符串
(定义(将十进制转换为基数)
(如果(精确(截断(/(对数)
(对数基((())))
(结果“))
(let*((电源(出口基本订单))
(数字(商值幂)))
(如果(零订单)
(字符串附加结果(列表参考转换数字))
(环路(-val(*数字功率))
(预订单)
(字符串附加结果(列表参考转换位数(()()()())))
; 将颜色转换为十六进制字符串
; '(255)=>“#ffffff”
(定义(颜色rgb到十六进制颜色)
(字符串附加“#”
(预焊盘编号
(将十进制转换为基数(汽车颜色)16)2“0”)
(预焊盘编号
(将十进制转换为基数(cadr颜色)16)2“0”)
(预焊盘编号
(将十进制转换为基数(caddr颜色)16)2“0”)
)
)
; Паааааааа六角
(设置!imgcolorHEX(颜色rgb到十六进制imgcolor))