Advertisement
RaZgRiZ

Color Conversion Lib (C.S) 11/2/15

Aug 8th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. // atan2/cos/sin functions operate in degrees, not radi
  2.  
  3. // Compresses RGB32 color to INT format
  4. // usage: /RGB32toINT #A #R #G #B [ action ]
  5. // INT variables (separate): $a $r $g $b
  6. RGB32_to_INT = [
  7.     local a r g b
  8.     a = (<< $arg1 24)
  9.     r = (<< $arg2 16)
  10.     g = (<< $arg3  8)
  11.     b =     $arg4
  12.     if (>= $numargs 5) [ doargs $arg5 ] [ + $a $r $g $b ]
  13. ]
  14.  
  15. // Converts INT color to RGB32 format
  16. // usage: /INT_to_RGB32 #INT [ action ]
  17. // RGB32 variables: $a $r $g $b
  18. INT_to_ARGB32 = [
  19.     local a r g b
  20.     a =    (>> $arg1 24)
  21.     r = (& (>> $arg1 16) 255)
  22.     g = (& (>> $arg1  8) 255)
  23.     b = (&     $arg1     255)
  24.     if (< $numargs 2) [ concat $a $r $g $b ] [ doargs $arg2 ]
  25. ]
  26.  
  27. // Converts RGB32 color to HEX data
  28. // usage: /RGB32_to_HEX #A #R #G #B #length [ action ]
  29. // HEX variables: $hex
  30. RGB32_to_HEX = [
  31.     local hex
  32.     hex = (tohex (
  33.         RGB32_to_INT $arg1 $arg2 $arg3 $arg4
  34.     ) (? $arg5 (min $arg5 16) 6))
  35.     if (< $numargs 6) [ result $hex ] [ doargs $arg6 ]
  36. ]
  37.  
  38. // Converts RGB24 color to CIELch data (CIELab data also available!)
  39. // usage: /RGB24_to_CIELCH #R #G #B [ action ]
  40. // CIELch variables: $l $c $h  | CIELab variables: $l $a $b
  41. RGB24_to_CIELCH = [
  42.     local x y z   a b   l c h
  43.     arg1 = (divf $arg1 255)
  44.     arg2 = (divf $arg2 255)
  45.     arg3 = (divf $arg3 255)
  46.     looplist i "arg1 arg2 arg3" [
  47.         if (>f $$i 0.04045) [
  48.             $i = (*f (pow (divf (+f $$i 0.055) 1.055) 2.4) 100)
  49.         ] [ $i = (*f (divf $$i 12.92) 100) ]
  50.     ]
  51.     x = (divf (+f (*f $arg1 0.4124) (*f $arg2 0.3576) (*f $arg3 0.1805))  95.047)
  52.     y = (divf (+f (*f $arg1 0.2126) (*f $arg2 0.7152) (*f $arg3 0.0722)) 100.000)
  53.     z = (divf (+f (*f $arg1 0.0193) (*f $arg2 0.1192) (*f $arg3 0.9505)) 108.883)
  54.     looplist i "x y z" [
  55.         if (>f $$i 0.008856) [
  56.             $i = (pow $$i (divf 1 3))
  57.         ] [ $i = (+f (*f 7.787 $$i) (divf 16 116)) ]
  58.     ]
  59.     a = (*f 500 (-f $x $y))
  60.     b = (*f 200 (-f $y $z))
  61.     l = (if (>f $y 0.008856) [-f (*f 116 $y) 16] [*f 903.3 $y])
  62.     c = (sqrt (+f (pow $a 2) (pow $b 2)))
  63.     h = (atan2 $b $a)
  64.     cond (<f $h 0) [ h = (+f $h 360) ] (>=f $h 360) [ h = (-f $h 360) ]
  65.     if (< $numargs 4) [ concat $l $c $h ] [ doargs $arg4 ]
  66. ]
  67.  
  68. // Converts CIELCH data to RGB24 format
  69. // usage: /CIELCH_to_RGB24 #R #G #B [ action ]
  70. // RGB24 variables: $r $g $b
  71. CIELCH_to_RGB24 = [
  72.     local x y z   r g b
  73.     y = (divf (+f $arg1 16) 116)
  74.     x = (+f (divf (*f (cos $arg3) $arg2) 500) $y)
  75.     z = (-f $y (divf (*f (sin $arg3) $arg2) 200))
  76.     looplist i "x y z" [
  77.         if (>f (pow $$i 3) 0.008856) [
  78.             $i = (pow $$i 3)
  79.         ] [ $i = (divf (-f $$i (divf 16 116)) 7.787) ]
  80.     ]
  81.     x = (divf (*f $x  95.047) 100)
  82.     z = (divf (*f $z 108.883) 100)
  83.     r = (+f (*f $x  3.2406) (*f $y -1.5372) (*f $z -0.4986))
  84.     g = (+f (*f $x -0.9689) (*f $y  1.8758) (*f $z  0.0415))
  85.     b = (+f (*f $x  0.0557) (*f $y -0.2040) (*f $z  1.0570))
  86.     looplist i "r g b" [
  87.         if (>f $$i 0.0031308) [
  88.             $i = (-f (*f 1.055 (pow $$i (divf 1 2.4))) 0.055)
  89.         ] [ $i = (*f 12.92 $$i) ]
  90.         $i = (round (*f $$i 255))
  91.     ]
  92.     if (< $numargs 4) [ concat $r $g $b ] [ doargs $arg4 ]
  93. ]
  94.  
  95. // Converts RGB24 color to HSV data
  96. // usage: /RGB24_to_HSV #R #G #B [ action ]
  97. // HSV variables: $h $s $v
  98. RGB24_to_HSV = [
  99.     local r g b mn d h s v
  100.     r  = (divf $arg1 0xFF)
  101.     g  = (divf $arg2 0xFF)
  102.     b  = (divf $arg3 0xFF)
  103.     v  = (maxf $r $g $b)
  104.     mn = (minf $r $g $b)
  105.     if (=f $mn $v) [
  106.         d = 0
  107.         h = 0
  108.         s = 0
  109.     ] [
  110.         d = (-f $v $mn)
  111.         s = (divf $d $v)
  112.         h = (casef $v $r [
  113.             floor (*f 60 (modf (divf (-f $g $b) $d) 6))
  114.         ] $g [
  115.             *f 60 (+f (divf (-f $b $r) $d) 2)
  116.         ] $b [
  117.             *f 60 (+f (divf (-f $r $g) $d) 4)
  118.         ])
  119.     ]
  120.     if (< $numargs 4) [ concat $h $s $v ] [ doargs $arg4 ]
  121. ]
  122.  
  123. // Converts HSV data to RGB24 format
  124. // usage: /HSV_to_RGB24 #H #S #V [ action ]
  125. // RGB24 variables: $r $g $b
  126. HSV_to_RGB24 = [
  127.     local c x m r g b
  128.     c = (*f $arg3 $arg2)
  129.     x = (*f $c (-f 1 (absf (-f (modf (divf $arg1 60) 2) 1))))
  130.     m = (-f $arg3 $c)
  131.     h = (div $arg1 60)
  132.     r = (round (*f 255 (+f (at [@c @x 0 0 @x @c] $h) $m)))
  133.     g = (round (*f 255 (+f (at [@x @c @c @x 0 0] $h) $m)))
  134.     b = (round (*f 255 (+f (at [0 0 @x @c @c @x] $h) $m)))
  135.     if (< $numargs 4) [ concat $r $g $b ] [ doargs $arg4 ]
  136. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement