将十六进制颜色转换为浮点数 [英] Convert hexidecimal colors to floats

查看:156
本文介绍了将十六进制颜色转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎有些愚蠢,但是我无法意识到如何在glColor4f中输入所需的颜色.我的意思是,它需要4个介于0.0到1.0之间的浮点数,并且我发现的所有颜色都是十六进制或rgb(介于0到255之间).

This may seem a bit silly, but I can't realize how I have to enter the desired color in glColor4f. I mean, it needs 4 floats between 0.0 ~ 1.0, and all the colors I find are in hexadecimal or in rgb (which are between 0 ~ 255).

如何使用glColor4f进行转换以获取所需的颜色?

How do I have to make the conversion to adquire the desired color using glColor4f?

我已经尝试做过这样的事情:

I have already tried doing something like this:

glColor4f(1/248, 1/50, 1/99);

但这并不能解决问题.

我正在使用JAVA和OpenGL-ES

I'm using JAVA and OpenGL-ES

推荐答案

'C'以整数进行整数算术

'C' does integer arithmatic in integers

1/248将向下舍入为零,您需要使用浮点值.即1.0/248.0
但是由于要将8bit RGB转换为0-1,因此需要执行(float)R/255.0

1/248 will round down to zero, you need to use floating point values. ie 1.0/248.0
But since you want to convert 8bit RGB into 0-1 you need to do(float)R/255.0

edit:无论使用哪种语言,glColor4f()期望的值都在0-1范围内,其中0是黑色,而1是全彩色.要将0-255的值转换为0-1的范围,您必须除以255,而不是取倒数.

edit: in whatever language glColor4f() expects a value in the range 0-1 where 0 is black and 1 is full color. To convert a 0-255 value into the range 0-1 you have to divide by 255, not take the reciprocal.

这篇关于将十六进制颜色转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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