Unable to set Cocos2d-x label font color

            rect = Sprite::create();
            rect->setTextureRect(Rect(0, 0, 180, 80));
    
            rect->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height + 80));
            auto grad = LayerGradient::create(arr[randSayi][0], arr[randSayi][1]);
            grad->changeHeight(rect->getContentSize().height);
            grad->changeWidth(rect->getContentSize().width);
    
            label = Label::createWithTTF(arrRenk[randRenk], "fonts/font.ttf", 54);
            label->setPosition(rect->getContentSize().width / 2, rect->getContentSize().height / 2);
            label->setColor(/*(Color3B)arr[randSayi][0]*/Color3B(arr[randSayi][0]));
            grad->addChild(label);
    
            rect->setTag(i);
            rect->addChild(grad);

This is how i crete a rectangular sprite with a gradient color and a label added on top of it. My problem is that i can’t set font color properly.

 label->setColor(/*(Color3B)arr[randSayi][0]*/Color3B(arr[randSayi][0]));

arr[][] is an array of colors Color4B. I both tried casting and the uncommented out but the result is:

enter image description here

Which means color isn’t set. Can anybody identify the problem?

Try using Label::setTextColor(Color4B) instead of Node::setColor(Color3B)

setTextColour() is the colour the text is rendered to texture with.

setColor() is the colour that texture (like sprite) is tinted in the shader.

Hope this helps

Any idea on it is very appreciated :smile:

I tried but still the same result. Might it be about parent-children relation order or simply the z-order?

From the code you’ve provided it’s unlikely to be an ordering issue. Hmmm. Have you tried debugging or logging the result of arr[randSayi][0] and Color3B(arr[randSayi][0]) to console? just to make sure the translation is correct.

Debug:
Breakpoint on label->setColor(...) and inspect your array then step in and inspect the Color3B param.

Log

auto c4b = arr[randSayi][0];
auto c3b = Color3B(arr[randSayi][0]);
CCLOG("c4b color: (%d,%d,%d,%d)", c4b.r, c4b.g, c4b.b, c4b.a);
CCLOG("c3b color: (%d,%d,%d)", c3b.r, c3b.g, c3b.b);

As far as I can see, everything is working fine.

You set your label color to arr[randSayi][0] which is the same color as the first color of the gradient.

Picking colors from your image, I get Color3B(117, 209 234) from the label which is exactly the same color as the topmost pixels of your gradient…

So what else color should the label be?