将Javascript源代码转换为Python时出现问题
我正在尝试将Javascript函数转换为Python。大部分都没有问题,但有一句话我不知道如何转换:将Javascript源代码转换为Python时出现问题,javascript,python,python-3.x,Javascript,Python,Python 3.x,我正在尝试将Javascript函数转换为Python。大部分都没有问题,但有一句话我不知道如何转换: color=+(“0x”+color.slice(1).replace(color.length将红色、绿色和蓝色值存储在单独的变量中 颜色=颜色。匹配(/^rgba?\(\d+)、\s*(\d+)、\s*(\d+)、\s*(\d+(?:\。\d+))$/); r=颜色[1]; g=颜色[2]; b=颜色[3]; }否则{ //如果十六进制-->将其转换为RGB:http://gist.git
color=+(“0x”+color.slice(1).replace(color.length<5&//g,$&$&'))
到目前为止,Python是:
color=+(“0x”+颜色[:1]。替换(
透镜(颜色)<5和//g,$&$&')
)
idk什么是+()
或//g
。完整的JS功能是:
功能灯或暗盒(彩色){
//红色、绿色和蓝色值的变量
var-r,g,b,hsp;
风险值阈值=127.5
//检查颜色的格式,十六进制还是RGB?
如果(颜色匹配(/^rgb/)){
//如果RGB-->将红色、绿色和蓝色值存储在单独的变量中
颜色=颜色。匹配(/^rgba?\(\d+)、\s*(\d+)、\s*(\d+)、\s*(\d+(?:\。\d+))$/);
r=颜色[1];
g=颜色[2];
b=颜色[3];
}否则{
//如果十六进制-->将其转换为RGB:http://gist.github.com/983661
color=+(“0x”+color.slice(1).replace(color.length<5&&//g,$&$&'))
console.log(彩色)
r=颜色>>16;
g=颜色>>8&255;
b=颜色&255;
}
//HSP(高灵敏度Poo)方程http://alienryderflex.com/hsp.html
hsp=Math.sqrt(
0.299*(r*r)+
0.587*(g*g)+
0.114*(b*b)
)
//使用HSP值,确定颜色是浅还是暗
返回hsp>threshold;
}
这个//g
是一个正则表达式,+()
将字符串强制转换成一个数字(并且0x
使其成为十六进制)。在Python中,您将使用re
模块和int()
内置函数来实现这一点
如果颜色是以缩写形式书写的,replace
会复制字符。Python的等价物是一个re.sub()
。在Python的正则表达式方言中,使用反斜杠而不是美元作为反向引用。所以\1
指的是第一个匹配组
>>> import re
>>> color = "#123456"
>>> re.sub("(.)", r"\1\1" if len(color)<5 else r"\1", color[1:])
'123456'
>>> color = "#abc"
>>> re.sub("(.)", r"\1\1" if len(color)<5 else r"\1", color[1:])
'aabbcc'
总而言之:
int(re.sub("(.)", r"\1\1" if len(color)<5 else r"\1", color[1:]), 16)
int(re.sub(“(”),r“\1\1”if len(color)这是一行相当紧凑的代码。让我们来解构它
首先,+()
与Number()
相同,在这种情况下,也可以使用parseInt()
来实现它
Next//g
只是一个正则表达式。它本身不做任何事情。它只是一个类似于“hello”
或3
的值。它是replace()
的第一个参数
因此,该行可以重写为:
let temp1 = color.slice(1); // In this case can also be rewritten as color.substr(1)
// It basically removes the first character
if (color.length < 5) {
temp1 = temp1.replace(/./g, '$&$&'); // This replaces each character with itself twice.
// For example "123" is replaced with "112233"
}
let temp2 = "0x" + temp1. // This adds "0x" to the previous string so that
// it will look like a hexadecimal number.
color = parseInt(temp2); // Convert string of hexadecimal number to a number.
let temp1=color.slice(1);//在这种情况下也可以重写为color.substr(1)
//它基本上删除了第一个字符
如果(颜色长度<5){
temp1=temp1.replace(///g,$&$&');//这会将每个字符自身替换两次。
//例如,“123”替换为“112233”
}
让temp2=“0x”+temp1//这将在前面的字符串中添加“0x”,以便
//它看起来像一个十六进制数。
color=parseInt(temp2);//将十六进制数的字符串转换为数字。
我个人不懂Python,但上面的内容应该很容易用任何语言重写
如果您不习惯使用正则表达式,可以使用循环重写加倍代码。以下是一种完全不使用任何奇特函数/方法的替代实现:
// Remove first character (color.slice(1))
let temp1 = "";
for (let i=1; i<color.length; i++) { // copy every character except color[0]
temp1 = temp1 + color[i];
}
// Convert 3-letter color to 6-letter color, eg #69a to #6699aa
if (color.length < 5) {
let temp2 = "";
for (let i=0; i<3; i++) {
temp2 = temp2 + temp1[i] + temp1[i];
}
temp1 = temp2;
}
let temp3 = "0x" + temp1. // This adds "0x" to the previous string so that
// it will look like a hexadecimal number.
color = parseInt(temp3); // Convert string of hexadecimal number to a number.
//删除第一个字符(color.slice(1))
让temp1=“”;
例如(设i=1;我要问的第一个问题是“它做什么?”因为您可以移植精确的代码,但是编写Python代码与JS代码做相同的事情要好得多,因为两个langauges的标准库之间存在差异。现在忽略Python代码:通过查看JS来解释JS做了什么。color
?既然如此,代码做什么?@Mike'Pomax'Kamermans当然可以,如果你对语法不感到困惑的话。我以前从未见过这种类型强制。这很不寻常,让我想起了lambda演算。我还忘了还有regexp对象。
// Remove first character (color.slice(1))
let temp1 = "";
for (let i=1; i<color.length; i++) { // copy every character except color[0]
temp1 = temp1 + color[i];
}
// Convert 3-letter color to 6-letter color, eg #69a to #6699aa
if (color.length < 5) {
let temp2 = "";
for (let i=0; i<3; i++) {
temp2 = temp2 + temp1[i] + temp1[i];
}
temp1 = temp2;
}
let temp3 = "0x" + temp1. // This adds "0x" to the previous string so that
// it will look like a hexadecimal number.
color = parseInt(temp3); // Convert string of hexadecimal number to a number.
color = +("0x" + color.slice(1).replace(color.length < 5 && /./g, '$&$&'))