Skip to content

RGB space

Hugo edited this page Sep 16, 2019 · 5 revisions

Basics

There are four representations of an RGB color in dufy:

  • lrgb: linear RGB. The range depends on RGB space but is typically [0, 1].
    • accepted type of lrgb-to- converters: real real real
    • return type of -to-lrgb converters: (values double-float double-float double-float &optional)
  • rgb: gamma-corrected RGB. The range depends on RGB space but is typically [0, 1].
    • accepted type of rgb-to- converters: real real real
    • return type of -to-rgb converters: (values double-float double-float double-float &optional)
  • qrgb: quantized RGB. The range depends on bits per channel but is typically {0, 1, ..., 255}.
    • accepted type of qrgb-to- converters: fixnum fixnum fixnum
    • return type of -to-qrgb converters: (values fixnum fixnum fixnum &optional)
  • rgbpack: RGB value packed to an integer. The range depends on bits per channel but is typically {#x000000, #x000001, ..., #xFFFFFF}.
    • accepted type of rgbpack-to- converters: integer
    • return type of -to-rgbpack converters: (values integer &optional)

The followings are the simplest examples:

(dufy:rgbpack-to-lrgb #xFF8800)
;; => 1.0d0               ; linear R value
;; 0.24620132670783548d0  ; linear G value
;; 0.0d0                  ; linear B value
(dufy:hsv-to-qrgb 120 0.5 1) ; Hue = 120°, Saturation = 0.5, Value = 1
;; => 128 ; quantized R value
;; 255    ; quantized G value
;; 128    ; quantized B value
(dufy:xyz-to-rgb 0.9505d0 1 1.0889d0) ; white point of illuminant D65
;; => 1.000053479346423d0 ; gamma-corrected R value
;; 0.9999860875272101d0   ; gamma-corrected G value
;; 0.9999802745645446d0   ; gamma-corrected B value

Most converters regard the implicit RGB space as sRGB. You can specify it explicitly.

(dufy:xyz-to-qrgb 0.37314 0.70144 1.0601 :rgbspace dufy:+srgb+ :clamp nil)  ; sRGB
;; => -169
;;    255
;;    255

(dufy:xyz-to-qrgb 0.37314 0.70144 1.0601 :rgbspace dufy:+adobe+ :clamp nil) ; Adobe RGB
;; => 2
;;    255
;;    255

Predefined RGB spaces

All the following symbols are exported by dufy/core and reexported by dufy.

Name Symbol Illuminant bpc Range of linear val.
sRGB +SRGB+ D65 8 [0.0, 1.0]
Adobe RGB +ADOBE+ D65 8 [0.0, 1.0]
Adobe RGB +ADOBE-16+ D65 16 [0.0, 1.0]
scRGB +SCRGB-16+ D65 16 [-0.5, 7.4999]
scRGB-nl +SCRGB-NL+ D65 12 [-0.6038, 7.5913]
bg-sRGB +BG-SRGB-16+ D65 16 [-0.53, 1.68]
bg-sRGB +BG-SRGB-12+ D65 12 [-0.53, 1.68]
bg-sRGB +BG-SRGB-10+ D65 10 [-0.53, 1.68]
ProPhoto RGB +PROPHOTO+ D50 8 [0.0, 1.0]
ProPhoto RGB +PROPHOTO-12+ D50 12 [0.0, 1.0]
ProPhoto RGB +PROPHOTO-16+ D50 16 [0.0, 1.0]
PAL/SECAM RGB +PAL/SECAM+ D65 8 [0.0, 1.0]
CIE RGB +CIE-RGB+ E 8 [0.0, 1.0]
NTSC RGB +NTSC1953+ C 8 [0.0, 1.0]
Wide Gamut RGB +WIDE-GAMUT+ D50 8 [0.0, 1.0]

Define your own RGB space

Function: MAKE-RGBSPACE XR YR XG YG XB YB &KEY (ILLUMINANT +ILLUM-D65+) (LMIN 0.0d0) (LMAX 1.0d0) (LINEARIZER (RCURRY #'FLOAT 1.0d0)) (DELINEARIZER (RCURRY #'FLOAT 1.0d0)) (BIT-PER-CHANNEL 8) (FORCE-NORMAL NIL)

In the following example, CIE RGB is defined with gamma value 2.2. (That of the predefined one is 1.0.)

(defparameter +my-cie-rgb+
  (dufy:make-rgbspace 0.7347d0 0.2653d0 0.2738d0 0.7174d0 0.1666d0 0.0089d0
                     :linearizer (dufy:gen-linearizer 2.2d0)
                     :delinearizer (dufy:gen-delinearizer 2.2d0)
                     :illuminant dufy:+illum-e+))
Clone this wiki locally