【发布时间】:2019-11-01 09:18:29
【问题描述】:
如何在Scheme中将十进制转换为十六进制?
需要在 GIMP 中为 JSON 转换 RGB 为 HEX:
(set! imgcolor (car (gimp-color-picker image newDraw 1 1 TRUE TRUE 1)))
在脚本中。现在的结果是 RGB 格式:(255 255 255)
【问题讨论】:
如何在Scheme中将十进制转换为十六进制?
需要在 GIMP 中为 JSON 转换 RGB 为 HEX:
(set! imgcolor (car (gimp-color-picker image newDraw 1 1 TRUE TRUE 1)))
在脚本中。现在的结果是 RGB 格式:(255 255 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))
【讨论】: