I'm trying to map my "quadruplet" object to an unsigned 32-bit number, that is 
supposed to represent an RGBA pixel. I'm able to convert my floating point 
quadruplet values to their respective R, G, B, and A components, but when I try 
to do an and on all of the components, I'm getting a 0. Here is my code:
    
    
    proc map*(x, a, b, p, q: float): float {.inline.} =
      return (x - a) * (q - p) / (b - a) + p
    
    #...
    
    type
      # [-1, 1] -> [0x00, 0xFF]
      Quadruplet* = ref object of RootObj
        w*: float
        x*: float
        y*: float
        z*: float
    
    
    # ...
    
    # Returns a quadruplet as a nicely formatted RGBA pixel.  Remember:
    # [-1, 1] -> [0x00, 0xFF].  Anything out of that range will be clamped
    proc toRGBA*(q: Quadruplet): uint32 {.inline.} =
      let
        r = q.x.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 24
        g = q.y.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 16
        b = q.z.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32 shl 8
        a = q.w.clamp(-1, 1).map(-1, 1, 0x00, 0xFF).uint32
        pixel = r and g and b and a
      
      echo ""
      echo r
      echo g
      echo b
      echo a
      echo pixel
      echo ""
      
      return pixel
    

Those echo statements are in there for debugging purposes right now.

So an example call would be: echo Quadruplet(w:0, x:1, y:1, z:1).toRGBA() # 50% 
white

And it's producing this on standard output 
    
    
    4278190080
    16711680
    65280
    127
    0
    

All of those components look correct, except for when I combine them with the 
ands into the full pixel. Can someone explain what's wrong here and how to fix 
it? 

Reply via email to