Merge pull request #29567 from geekpi/translating

translated
This commit is contained in:
geekpi 2023-06-19 08:40:53 +08:00 committed by GitHub
commit 8564fc801a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 217 additions and 216 deletions

View File

@ -1,216 +0,0 @@
[#]: subject: "Make a web-safe color guide with Bash"
[#]: via: "https://opensource.com/article/23/4/web-safe-color-guide-bash"
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Make a web-safe color guide with Bash
======
When computer displays had a limited color palette, web designers often used a set of [web-safe colors][1] to create websites. While modern websites displaying on newer devices can display many more colors than the original web-safe color palette, I sometimes like to refer to the web-safe colors when I create web pages. This way I know my pages look good anywhere.
You can find web-safe color palettes on the web, but I wanted to have my own copy for easy reference. And you can make one too, using the `for` loop in Bash.
### Bash for loop
The syntax of a [for loop in Bash][2] looks like this:
> for _variable_ in _set_ ; do _statements_ ; done
As an example, say you want to print all numbers from 1 to 3. You can write a quick `for` loop on the Bash command line to do that for you:
```
$ for n in 1 2 3 ; do echo $n ; done
1
2
3
```
The semicolons are a standard Bash statement separator. They let you write multiple commands on a single line. If you were to include this `for` loop in a Bash script file, you might instead replace the semicolons with line breaks and write out the `for` loop like this:
```
for n in 1 2 3
do
echo $n
done
```
I like to include the `do` on the same line as the `for` so it's easier for me to read:
```
for n in 1 2 3 ; do
echo $n
done
```
### More than one for loop at a time
You can put one loop inside another. That can help you to iterate over several variables, to do more than one thing at a time. Let's say you wanted to print out all combinations of the letters A, B, and C with the numbers 1, 2, and 3. You can do that with two `for` loops in Bash, like this:
```
#!/bin/bash
for number in 1 2 3 ; do
for letter in A B C ; do
echo $letter$number
done
done
```
If you put these lines in a Bash script file called `for.bash` and run it, you see nine lines showing the combinations of all the letters paired with each of the numbers:
```
$ bash for.bash
A1
B1
C1
A2
B2
C2
A3
B3
C3
```
### Looping through the web-safe colors
The web-safe colors are all colors from hexadecimal color `#000` (black, where the red, green, and blue values are all zero) to `#fff` (white, where the red, green, and blue colors are all at their full intensities), stepping through each hexadecimal value as 0, 3, 6, 9, c, and f.
You can generate a list of all combinations of the web-safe colors using three `for` loops in Bash, where the loops iterate over the red, green, and blue values.
```
#!/bin/bash
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "#$r$g$b"
done
done
done
```
If you save this in a new Bash script called `websafe.bash` and run it, you see an iteration of all the web safe colors as hexadecimal values:
```
$ bash websafe.bash | head
#000
#003
#006
#009
#00c
#00f
#030
#033
#036
#039
```
To make an HTML page that you can use as a reference for web-safe colors, you need to make each entry a separate HTML element. Put each color in a `<div>` element, and set the background to the web-safe color. To make the hexadecimal value easier to read, put it inside a separate `<code>` element. Update the Bash script to look like this:
```
#!/bin/bash
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "<div style='background-color:#$r$g$b'><code>#$r$g$b</code></div>"
done
done
done
```
When you run the new Bash script and save the results to an HTML file, you can view the output in a web browser to all the web-safe colors:
```
$ bash websafe.bash > websafe.html
```
![Colour gradient.][3]
The web page isn't very nice to look at. The black text on a dark background is impossible to read. I like to apply some HTML styling to ensure the hexadecimal values are displayed with white text on a black background inside the color rectangle. To make the page look really nice, I also use HTML grid styles to arrange the boxes with six per row and some space between each box.
To add this extra styling, you need to include the other HTML elements before and after the for loops. The HTML code at the top defines the styles and the HTML code at the bottom closes all the open HTML tags:
```
#!/bin/bash
cat<<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web-safe colors</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
div {
padding-bottom: 1em;
}
code {
background-color: black;
color: white;
}
@media only screen and (min-width:600px) {
body {
display: grid;
grid-template-columns: repeat(6,1fr);
column-gap: 1em;
row-gap: 1em;
}
div {
padding-bottom: 3em;
}
}
</style>
</head>
</body>
EOF
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "<div
style='background-color:#$r$g$b'><code>#$r$g$b</code></div>"
done
done
done
cat<<EOF
</body>
</html>
EOF
```
This finished Bash script generates a web-safe color guide in HTML. Whenever you need to refer to the web-safe colors, run the script and save the results to an HTML page. Now you can see a representation of the web-safe colors in your browser as an easy-reference guide for your next web project:
```
$ bash websafe.bash > websafe.html
```
![Web colors.][4]
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/4/web-safe-color-guide-bash
作者:[Jim Hall][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/jim-hall
[b]: https://github.com/lkxed/
[1]: https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors
[2]: https://opensource.com/article/19/6/how-write-loop-bash
[3]: https://opensource.com/sites/default/files/2023-03/10000001000002620000013685924861376B1AB6.webp
[4]: https://opensource.com/sites/default/files/2023-03/10000001000002620000013633233DC8DC56C891.webp

View File

@ -0,0 +1,217 @@
[#]: subject: "Make a web-safe color guide with Bash"
[#]: via: "https://opensource.com/article/23/4/web-safe-color-guide-bash"
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
使用 Bash 制作 Web 安全的颜色指南
======
当计算机显示器的调色板有限时,网页设计师通常使用一组 [Web 安全颜色][1]来创建网站。虽然在较新设备上显示的现代网站可以显示比最初的 Web 安全调色板更多的颜色,但我有时喜欢在创建网页时参考 Web 安全颜色。这样我就知道我的网页在任何地方都看起来不错。
你可以在网上找到 Web 安全调色板,但我想拥有自己的副本以方便参考。你也可以使用 Bash 中的 `for` 循环创建一个。
### Bash for 循环
[Bash 中的 for 循环][2]的语法如下所示:
> for _variable_ in _set_ ; do _statements_ ; done
例如,假设你想打印从 1 到 3 的所有数字。你可以快速在 Bash 命令行上编写一个 `for` 循环来为你完成这项工作:
```
$ for n in 1 2 3 ; do echo $n ; done
1
2
3
```
分号是标准的 Bash 语句分隔符。它们允许你在一行中编写多个命令。如果你要在 Bash 脚本文件中包含这个 `for` 循环,你可以用换行符替换分号并像这样写出 `for` 循环:
```
for n in 1 2 3
do
echo $n
done
```
我喜欢将 `do``for` 放在同一行,这样我更容易阅读:
```
for n in 1 2 3 ; do
echo $n
done
```
### 一次多个 for 循环
你可以将一个循环放在另一个循环中。这可以帮助你迭代多个变量,一次做不止一件事。假设你想打印出字母 A、B 和 C 与数字 1、2 和 3 的所有组合。你可以在 Bash 中使用两个 `for` 循环来实现,如下所示:
```
#!/bin/bash
for number in 1 2 3 ; do
for letter in A B C ; do
echo $letter$number
done
done
```
如果将这些行放在名为 `for.bash` 的 Bash 脚本文件中并运行它,你会看到九行显示了所有字母与每个数字配对的组合:
```
$ bash for.bash
A1
B1
C1
A2
B2
C2
A3
B3
C3
```
### 遍历 Web 安全颜色
Web 安全颜色是从十六进制颜色 `#000`(黑色,即红色、绿色和蓝色值均为零)到 `#fff`(白色,即红色、绿色和蓝色均为最高),每个十六进制值的步进为 0、3、6、9、c 和 f。
你可以在 Bash 中使用三个 `for` 循环生成 Web 安全颜色的所有组合的列表,其中循环遍历红色、绿色和蓝色值。
```
#!/bin/bash
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "#$r$g$b"
done
done
done
```
如果将其保存在名为 `websafe.bash` 的新 Bash 脚本中并运行它,你就会看到所有 Web 安全颜色的十六进制值的迭代:
```
$ bash websafe.bash | head
#000
#003
#006
#009
#00c
#00f
#030
#033
#036
#039
```
要制作可用作 Web 安全颜色参考的 HTML 页面,你需要使每个条目成为一个单独的 HTML 元素。将每种颜色放在一个 `<div>` 元素中,并将背景设置为 Web 安全颜色。为了使十六进制值更易于阅读,将其放在单独的 `<code>` 元素中。将 Bash 脚本更新为如下:
```
#!/bin/bash
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "<div style='background-color:#$r$g$b'><code>#$r$g$b</code></div>"
done
done
done
```
当你运行新的 Bash 脚本并将结果保存到 HTML 文件时,你可以在浏览器中查看所有 Web 安全颜色的输出:
```
$ bash websafe.bash > websafe.html
```
![Colour gradient.][3]
这个网页不是很好看。深色背景上的黑色文字无法阅读。我喜欢应用一些 HTML 样式来确保十六进制值在颜色矩形内以黑色背景上的白色文本显示。为了使页面看起来非常漂亮,我还使用 HTML 网格样式来排列每行六个框,每个框之间留出一些空间。
要添加这种额外的样式,你需要在 for 循环前后包含其他 HTML 元素。顶部的 HTML 代码定义样式,底部的 HTML 代码关闭所有打开的 HTML 标签:
```
#!/bin/bash
cat<<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web-safe colors</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
div {
padding-bottom: 1em;
}
code {
background-color: black;
color: white;
}
@media only screen and (min-width:600px) {
body {
display: grid;
grid-template-columns: repeat(6,1fr);
column-gap: 1em;
row-gap: 1em;
}
div {
padding-bottom: 3em;
}
}
</style>
</head>
</body>
EOF
for r in 0 3 6 9 c f ; do
for g in 0 3 6 9 c f ; do
for b in 0 3 6 9 c f ; do
echo "<div
style='background-color:#$r$g$b'><code>#$r$g$b</code></div>"
done
done
done
cat<<EOF
</body>
</html>
EOF
```
这个完成的 Bash 脚本以 HTML 格式生成 Web 安全颜色指南。每当你需要引用网络安全颜色时,运行脚本并将结果保存到 HTML 页面。现在你可以在浏览器中看到 Web 安全颜色的演示,作为你下一个 Web 项目的简单参考:
```
$ bash websafe.bash > websafe.html
```
![Web colors.][4]
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/4/web-safe-color-guide-bash
作者:[Jim Hall][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/jim-hall
[b]: https://github.com/lkxed/
[1]: https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors
[2]: https://opensource.com/article/19/6/how-write-loop-bash
[3]: https://opensource.com/sites/default/files/2023-03/10000001000002620000013685924861376B1AB6.webp
[4]: https://opensource.com/sites/default/files/2023-03/10000001000002620000013633233DC8DC56C891.webp