iosmacossprite-kitnscolorspace

Comparing SKColor of two nodes for both OSX and iOS?


I'm working on a subclass of SKLabelNode allowing line-breaks through the use of the new-line character \n. It is coming along nicely and I am currently in the process of making sure it works for OSX as well as iOS before preparing a podspec.

On the surface it seems to be working, but one of my tests is failing when building for OSX, despite passing for iOS. It is somehow NSColorSpace related but this is uncharted territory for me. This is the method in the class that copies the color to the subnodes:

- (void)setFontColor:(SKColor *)fontColor{
    [super setFontColor: fontColor];
    self.propertyStateholderNode.fontColor    = fontColor;
    for (SKLabelNode *subNode in self.subNodes) {
        subNode.fontColor    = fontColor;
    }
    _fontColor    = fontColor;
}

and this is the test that is failing, (I've removed tests towards other properties which passes on both platforms):

- (void)testThatSubnodesInheritsPropertiesFromParent{
    NORLabelNode *threeLineNode    = [self nodeWithThreeSubNodes];
    threeLineNode.fontColor    = [SKColor greenColor];

    for (SKLabelNode *subNode in threeLineNode.subNodes) {
        XCTAssertEqualObjects(threeLineNode.fontColor, subNode.fontColor, @"The subnodes should have the same fontColor as the parent.");
    }
}

Details for the test failing are as follows: ((threeLineNode.fontColor) equal to (subNode.fontColor)) failed: ("NSCalibratedRGBColorSpace 0 1 0 1") is not equal to ("NSDeviceRGBColorSpace 0 0.976895 0 1") ...

It is not clear to me at all how the nodes wind up with different colorspaces...


Solution

  • Since we know that the color spaces are different I would try converting them to the same color space before comparing using -colorUsingColorSpace:(NSColorSpace *). You can resolve them to one known colorspace or convert them to each other’s color spaces. Again only on Mac is this happening and is NSColorSpace available.

    A post on the CocoaBuilder mailing list talks about comparing the component values, but that is not ideal because the components do not distinguish rgb versus grayscale.