Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color "not valid for the NSColor Generic Gray Gamma" when creating SKTexture from GKNoise with gradientColors

This basic code works until I try to add gradientColors:

let noise = GKNoise(src)
noise.gradientColors = [ 0.0: NSColor.blue, 0.5: NSColor.green, 0.75: NSColor.white]
let map = GKNoiseMap(noise,
                     size: vector_double2(x: inParams.noiseSize, y: inParams.noiseSize),
                     origin: vector_double2(x:0, y:0),
                     sampleCount: vector_int2(x: Int32(inParams.size), y:Int32(inParams.size)),
                     seamless: true)
let tex = SKTexture(noiseMap: map)

At which point I get the following in the Xcode console, upon creating the SKTexture:

*** -getRed:green:blue:alpha: not valid for the NSColor Generic Gray Gamma 2.2 Profile colorspace 1 1; need to first convert colorspace.

Other people seem to be able to do this, so I’m not sure what I’m doing differently. I can’t figure out where I might set the color space.

macOS 11.3, Xcode 12.5, SwiftUI app.

like image 853
Rick Avatar asked Sep 02 '25 01:09

Rick


1 Answers

GameplayKit is invoking getRed(_:green:blue:alpha:) on each of your NSColor instances behind the scenes, and according to the documentation, this method only works on colors in the calibratedRGB or deviceRGB color spaces (although it appears to also work for other spaces, like sRGB).

NSColor.blue and NSColor.green use sRGB by default, but .white uses NSColorSpace.genericGray. To convert:

let white = NSColor.white.usingColorSpace(.sRGB)!

Alternatively, instantiating a color with its components directly, e.g. NSColor(red: 1, green: 1, blue: 1, alpha: 1), will put it in sRGB.

like image 67
McKinley Avatar answered Sep 09 '25 17:09

McKinley