-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss-luma.html
161 lines (161 loc) · 4.91 KB
/
css-luma.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width,initial-scale=1.0,min-scale=1.0,max-scale=1.0"
/>
<title>CSS如何根据背景色深浅切换文字颜色?</title>
<style>
.bgaa {
height: 100px;
line-height: 100px;
text-align: center;
background-color: black;
color: #fff;
}
.bgbox {
height: 100px;
line-height: 100px;
color: blue;
background-color: currentColor;
}
.bgbox > .bgbox-text {
text-align: center;
filter: grayscale(1) contrast(999) invert(1);
}
:root {
--red: 44;
--green: 135;
--blue: 255;
--threshold: 0.5;
--border-threshold: 0.8;
}
.bgbox-css-calc {
height: 100px;
line-height: 100px;
text-align: center;
background-color: rgb(var(--red), var(--green), var(--blue));
--r: calc(var(--red) * 0.2126);
--g: calc(var(--green) * 0.7152);
--b: calc(var(--blue) * 0.0722);
--sum: calc(var(--r) + var(--g) + var(--b));
--lightness: calc(var(--sum) / 255);
color: hsl(
0,
0%,
calc((var(--lightness) - var(--threshold)) * -999999%)
);
--border-alpha: calc((var(--lightness) - var(--threshold)) * 100);
border: 0.2em solid;
border-color: rgba(
calc(var(--red) - 50),
calc(var(--green) - 50),
calc(var(--blue) - 50),
var(--border-alpha)
);
}
.bgbox-num,
.bgbox-num1 {
display: flex;
flex-direction: row;
}
num {
width: 200px;
text-align: center;
font-size: 26px;
/* color: #aaa; */
--s: clamp(0%, (var(--num) - 99) * 99%, 85%);
color: hsl(27 var(--s) 36%);
}
num::before,
num1::before,
num2::before {
counter-reset: num var(--num);
content: counter(num);
/* display: block; */
}
num::after,
num1::after,
num2::after {
content: "阅读";
font-weight: bold;
}
num1 {
width: 200px;
text-align: center;
font-size: 26px;
/* 30 128 255 -> 245 63 63 */
--r: clamp(30, (var(--num) - 99) * 999 + 30, 245);
--g: clamp(63, (var(--num) - 100) * -999 + 63, 128);
--b: clamp(63, (var(--num) - 100) * -999 + 63, 255);
color: rgb(var(--r), var(--g), var(--b));
}
num2 {
width: 200px;
text-align: center;
font-size: 26px;
background: linear-gradient(rgb(244 67 54), rgb(244 67 54)),
linear-gradient(rgb(29 125 250), rgb(29 125 250));
color: transparent;
background-size: calc((var(--num) - 99) * 100%), 100%;
-webkit-background-clip: text;
background-clip: text;
}
.bar {
display: flex;
background-color: #a0a0a0;
}
.bar::before {
counter-reset: percent-bar var(--percent);
content: counter(percent-bar) "%\2002";
white-space: nowrap;
display: flex;
justify-content: end;
width: calc(var(--percent) * 1%);
background: linear-gradient(red, red), linear-gradient(orange, orange),
linear-gradient(yellow, yellow), linear-gradient(green, green);
color: #fff;
background-size: calc((var(--percent) - 90) * 100%) 100%,
calc((var(--percent) - 60) * 100%) 100%,
calc((var(--percent) - 30) * 100%) 100%, 100% 100%;
--offset: clamp(0%, (var(--percent) - 10) * -120%, 120%);
text-indent: var(--offset);
--lightness: clamp(35%, (var(--percent) - 10) * 999%, 100%);
color: hsl(0, 100%, var(--lightness));
}
</style>
</header>
<body>
<div class="bgaa">啊啊啊啊啊</div>
<div class="bgbox">
<div class="bgbox-text">css滤镜 - 只适用于黑白变化</div>
</div>
<div class="bgbox-css-calc">css计算属性组合</div>
<div class="bgbox-num">
<h3>灰色 -> 自定义颜色 clamp</h3>
<num style="--num: 1"></num>
<num style="--num: 99"></num>
<num style="--num: 120"></num>
</div>
<div class="bgbox-num1">
<h3>绝对颜色控制:自定义颜色 -> 自定义颜色 clamp</h3>
<num1 style="--num: 1"></num1>
<num1 style="--num: 99"></num1>
<num1 style="--num: 120"></num1>
</div>
<num2 style="--num: 100"></num2>
<div class="bar" style="--percent: 50"></div>
<script>
let _div = document.querySelector(".bgaa");
let r = 24,
g = 24,
b = 24;
let luma = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 255;
_div.style.backgroundColor = `rgba(${r},${g},${b},1)`;
_div.style.color = luma >= 0.5 ? "#000" : "#fff";
</script>
</body>
</html>