我们如何反转十六进制颜色代码? [英] How can we invert hex-color code?

查看:146
本文介绍了我们如何反转十六进制颜色代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web API将为我的视图发送背景颜色,背景颜色范围从白色 #ffffff 到黑色#000000 。因此,我不能为信息文本设置任何固定文本颜色。
设置文字颜色的最佳方法是什么?
我正在考虑反转背景颜色并将其设置为我的文本颜色。但是我不知道如何反转任何颜色或十六进制颜色代码。

My Web API will send the background color for my views, Background color range is from white #ffffff to black #000000. So I can not set any fix text color for my information text. What is the best way to set my text color? I'm thinking to invert the background color and set it as my text color. But I don't know How can I invert Any color or Hex color code.

例如,如果我的网站(背景)颜色是#00ff11,那么我的文本颜色会是#ff00ee。

为此,我在堆栈上搜索,但未找到任何颜色转换方法。

For this, I search over stack but did not find any method for color conversion.

谢谢

推荐答案

尝试一下。

这将使rgb具有十六进制颜色。现在您可以按如下所示反转颜色。

This will give rgb of your hex color. Now you can invert your colors as below.

int invertColor(String myColorString) {

    int color = (int)Long.parseLong(myColorString, 16);
    int r = (color >> 16) & 0xFF;
    int g = (color >> 8) & 0xFF;
    int b = (color >> 0) & 0xFF;
    int invertedRed = 255 - r;
    int invertedGreen = 255 - g;
    int invertedBlue = 255 - b;

    int invertedColor = Color.rgb(invertedRed, invertedGreen, invertedBlue);

    return invertedColor.toString();
}

这篇关于我们如何反转十六进制颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆