title: How to get correct RGB color values from PNG with PLTE palette using NSBitmapImageRep in Swift?
body: <p>Given a PNG image, I would like to get the RGB color at a given pixel using Swift on macOS.
  This is simply done by:</p>
  <pre class="lang-swift prettyprint-override"><code>let d = Data(base64Encoded: &quot;iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQImWP4z8AAAAMBAQCc479ZAAAAAElFTkSuQmCC&quot;)!
  let nsImage = NSImage(data: d)!
  let bitmapImageRep = NSBitmapImageRep(cgImage: nsImage.cgImage(forProposedRect: nil, context: nil, hints: [:])!)
  let color = bitmapImageRep.colorAt(x: 0, y: 0)
  print(color!.redComponent, color!.greenComponent, color!.blueComponent, separator: &quot; &quot;)
  // 1.0 0.0 0.0
  </code></pre>
  <p>This minimal example contains a one pixel PNG in RGB encoding with a red pixel in base64 encoding to make this easily reproducible.</p>
  <pre class="lang-sh prettyprint-override"><code>printf iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQImWP4z8AAAAMBAQCc479ZAAAAAElFTkSuQmCC | base64 --decode | pngcheck -v
  File: stdin (140703128616967 bytes)
    chunk IHDR at offset 0xfffffffffffffffb, length 13
      1 x 1 image, 24-bit RGB, non-interlaced
    chunk IDAT at offset 0xfffffffffffffffb, length 12
      zlib: deflated, 256-byte window, default compression
    chunk IEND at offset 0xfffffffffffffffb, length 0
  No errors detected in stdin (3 chunks, -133.3% compression).
  </code></pre>
  <p>When changing this PNG to an indexed PNG with a <a href="http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.PLTE" rel="nofollow noreferrer">PLTE Palette</a></p>
  <pre class="lang-sh prettyprint-override"><code>printf iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEX/AAAZ4gk3AAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg== | base64 --decode | pngcheck -v
  File: stdin (140703128616967 bytes)
    chunk IHDR at offset 0xfffffffffffffffb, length 13
      1 x 1 image, 8-bit palette, non-interlaced
    chunk PLTE at offset 0xfffffffffffffffb, length 3: 1 palette entry
    chunk IDAT at offset 0xfffffffffffffffb, length 10
      zlib: deflated, 256-byte window, default compression
    chunk IEND at offset 0xfffffffffffffffb, length 0
  No errors detected in stdin (4 chunks, -600.0% compression).
  </code></pre>
  <p>the snippet above changes to</p>
  <pre class="lang-swift prettyprint-override"><code>let d = Data(base64Encoded: &quot;iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEX/AAAZ4gk3AAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg==&quot;)!
  let nsImage = NSImage(data: d)!
  let bitmapImageRep = NSBitmapImageRep(cgImage: nsImage.cgImage(forProposedRect: nil, context: nil, hints: [:])!)
  let color = bitmapImageRep.colorAt(x: 0, y: 0)
  print(color!.redComponent, color!.greenComponent, color!.blueComponent, separator: &quot; &quot;)
  // 0.984313725490196 0.0 0.027450980392156862
  </code></pre>
  <p>It produces the unexpected output with a red component close to one and some non-zero blue component. Where does this change in color come from?</p>