微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[cocos2d-x]string 转 Color

static int toColor (const char* value,int index) {
            char digits[3];
            char *error;
            int color;
            
            if (strlen(value) != 8) return -1;
            value += index * 2;
            
            digits[0] = *value;
            digits[1] = *(value + 1);
            digits[2] = '\0';
            color = (int)strtoul(digits,&error,16);
            if (*error != 0) return -1;
            return color;
        }
        
        cocos2d::Color4B stringToColor(std::string &str)
        {
            if (str.length() != 8)
            {
                return cocos2d::Color4B::WHITE;
            }
            const char *ch = str.c_str();
            glubyte r = (glubyte)toColor(ch,0);
            glubyte g = (glubyte)toColor(ch,1);
            glubyte b = (glubyte)toColor(ch,2);
            glubyte a = (glubyte)toColor(ch,3);
            
            return cocos2d::Color4B(r,g,b,a);
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐