js获取字体颜色

JavaScript012

js获取字体颜色,第1张

console.log(getComputedStyle(ttitle[i]).color)

if (getComputedStyle(ttitle[i]).color=="rgb(0, 0, 0)") {

ttitle[i].style.color ='#10adff'

}else{

ttitle[i].style.color ='#000'

}

需要个字体设置颜色让rgb=000

.ttitle{

text-align: left

color: #000

}

调用的例子:

console.log(ColorLuminance(colorRGB(),0.5))  //先自动生成16进制颜色,在转成比原有颜色轻50%;

//16进制随机颜色

        function colorRGB(){

            return '#' + (function (h) {

                return new Array(7 - h.length).join("0") + h

            })((Math.random() * 0x1000000 << 0).toString(16))

        }

ColorLuminance接受两个参数:

参数1: 十六进制颜色值,例如“#abc”或“#123456”(散列是可选的)

参数2: 亮度因数,即,-0.1是10%更暗,0.2是轻20%等。

function ColorLuminance(hex, lum) {

            // validate hex string

            hex = String(hex).replace(/[^0-9a-f]/gi, '')

            if (hex.length < 6) {

                hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2]

            }

            lum = lum || 0

            // convert to decimal and change luminosity

            var rgb = "#", c, i

            for (i = 0 i < 3 i++) {

                c = parseInt(hex.substr(i*2,2), 16)

                c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16)

                rgb += ("00"+c).substr(c.length)

            }

            return rgb

        }

相关资源:https://blog.csdn.net/dingshi7798/article/details/105829757

1.首先架构一下网页的样式,既然是举例,那么就简单写入四个 span 用来做我们的文字存放,和之后的对比

2.我们首先用最初的 css 方式来给这些文字添加样式直接用 color 这个属性就可以控制文字的颜色就可以看到生效之后的效果

3.但是如果有很多不同的,颜色组成还可以这么写嘛,答案肯定是不行的。那么我们就用 js 来控制首先写一个 script 标签;里面 定义一个 数组 colors,存放 值 也就是 文字;而 下标 就可用来表示 颜色;

4.然后我们用一个循环来将,下标 和 分别存放到 span 标签中去;然后就会将三个不同的 span 写入页面;(第四个 span 是间隔用的 并无什么意义)

5.然后我们对比一下两个不同方式的页面代码量,咋一看 感觉并没什么差别,但是如果,颜色更多那???,图一只需要添加几个 下标 和值 ,但是图二那?(图一那个 span 并无意义)。