我有一个函数(不是我自己的),它需要0x*格式的十六进制值作为 colored颜色 值.我拥有的函数接受RGB值并将其转换为十六进制,但将其作为字符串输出.有没有办法将该字符串作为十六进制值进行计算?
我已经try 将十六进制值更改为整数,但该函数似乎不接受整数.
这是我拥有的函数,它获取RGB值并将其转换为十六进制.
def decToHexa(n):
# char array to store hexadecimal number
hexaDeciNum = ['0'] * 100
# Counter for hexadecimal number array
i = 0
while (n != 0):
# Temporary variable to store remainder
temp = 0
# Storing remainder in temp variable.
temp = n % 16
# Check if temp < 10
if (temp < 10):
hexaDeciNum[i] = chr(temp + 48)
i = i + 1
else:
hexaDeciNum[i] = chr(temp + 55)
i = i + 1
n = int(n / 16)
hexCode = ""
if (i == 2):
hexCode = hexCode + hexaDeciNum[0]
hexCode = hexCode + hexaDeciNum[1]
elif (i == 1):
hexCode = "0"
hexCode = hexCode + hexaDeciNum[0]
elif (i == 0):
hexCode = "00"
# Return the equivalent
# hexadecimal color code
return hexCode
# Function to convert the
# RGB code to Hex color code
def convertRGBtoHex(R, G, B):
if ((R >= 0 and R <= 255) and
(G >= 0 and G <= 255) and
(B >= 0 and B <= 255)):
hexCode = "0x";
hexCode = hexCode + decToHexa(R)
hexCode = hexCode + decToHexa(G)
hexCode = hexCode + decToHexa(B)
return hexCode
# The hex color code doesn't exist
else:
return "-1"
下面是使用该值的位置:
def appendCharacter(self,ch,x,y,r,g,b):
idx = self.numChars
M = self.metrics[ch]
self.textData[idx].screenXY = (x << 16) | y
self.textData[idx].atlasXY = (M["x"]<<16) | M["y"]
self.textData[idx].ascentDescentWidthLeftBearing = (
(M["ascent"]<<24) |
(M["descent"]<<16) |
(M["width"]<<8) |
(M["leftbearing"]+128) #make positive
)
self.textData[idx].color = int(convertRGBtoHex(r, g, b).lower(), 0)
self.numChars+=1
我希望我能提供更多的信息,但我拥有的大部分代码都是由我的教授提供的,所以我对它们是如何协同工作的知之甚少.