Advertisement
Guest User

full-bitpal

a guest
Jun 21st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #lang racket
  2. (require 2htdp/image)
  3. (define (subbits mask num)
  4. (if (or (odd? mask) (zero? mask))
  5. (bitwise-and mask num)
  6. (subbits (arithmetic-shift mask -1)
  7. (arithmetic-shift num -1))))
  8. (define-values (above* beside*)
  9. (let ([helper
  10. (λ (f mt . xs)
  11. (cond
  12. [(empty? xs) mt]
  13. [(empty? (rest xs)) (first xs)]
  14. [else (apply f xs)]))])
  15. (values (curry helper above empty-image)
  16. (curry helper beside empty-image))))
  17. (define split-list-by-count
  18. (letrec ([split-list-by-count-helper
  19. (lambda (n l r)
  20. (cond
  21. [(empty? r) l]
  22. [(>= (length (first l)) n)
  23. (split-list-by-count-helper n (cons `() l) r)]
  24. [else
  25. (split-list-by-count-helper
  26. n (cons (cons (first r) (first l)) (rest l)) (rest r))]))])
  27. (lambda (n r)
  28. (reverse (map reverse (split-list-by-count-helper n `(()) r))))))
  29. (define (square-full-of . images)
  30. (apply above*
  31. (map (curry apply beside*)
  32. (split-list-by-count
  33. (expt 2 (ceiling (/ (inexact->exact
  34. (floor (/ (log (length images))
  35. (log 2)))) 4)))
  36. images))))
  37. (define (palette
  38. (red #xff0000)
  39. (green #x00ff00)
  40. (blue #x0000ff))
  41. (let* ([white (bitwise-ior red green blue)]
  42. [sqsize (inexact->exact
  43. (min (max (floor (/ 256 (sqrt (+ 1 white))))
  44. 1) 24))])
  45. (lambda colors
  46. (apply square-full-of
  47. (map (lambda (x)
  48. (square sqsize 'solid
  49. (color (floor (/ (* (subbits red x) #xff)
  50. (if (zero? red) 1
  51. (subbits red white))))
  52. (floor (/ (* (subbits green x) #xff)
  53. (if (zero? green) 1
  54. (subbits green white))))
  55. (floor (/ (* (subbits blue x) #xff)
  56. (if (zero? blue) 1
  57. (subbits blue white)))))))
  58. colors)))))
  59. (define (nat->bitlist n (l `()))
  60. (if (zero? n) l
  61. (nat->bitlist (arithmetic-shift n -1)
  62. (cons (odd? n) l))))
  63. (define (bitlist->nat l (n 0))
  64. (if (empty? l) n
  65. (bitlist->nat (rest l)
  66. (bitwise-ior (if (first l) 1 0)
  67. (arithmetic-shift n 1)))))
  68. (define (color-component-bitcounts bits)
  69. (case bits
  70. [(0) `(0 0 0)]
  71. [(1) `(1 0 0)]
  72. [(2) `(1 1 0)]
  73. [else
  74. (let ([field-width (/ bits 3)])
  75. (case (modulo bits 3)
  76. [(0) (list field-width field-width field-width)]
  77. [(1) (list (floor field-width)
  78. (ceiling field-width)
  79. (floor field-width))]
  80. [else (list (ceiling field-width)
  81. (ceiling field-width)
  82. (floor field-width))]))]))
  83. (define (color-component-bitmasks bits)
  84. (let* ([affs
  85. (map (curryr build-list (lambda (_) #t))
  86. (color-component-bitcounts bits))]
  87. [negs (map (curry map not) affs)])
  88. (map bitlist->nat
  89. (list
  90. (append (first affs) (second negs) (third negs))
  91. (append (first negs) (second affs) (third negs))
  92. (append (first negs) (second negs) (third affs))))))
  93. (define (bitpal bits)
  94. (apply palette (color-component-bitmasks bits)))
  95. (define (full-bitpal bits)
  96. (apply (bitpal bits) (build-list (arithmetic-shift 1 bits) identity)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement