aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Engineering
  • Technology

Make A Web-Safe Color Guide With Bash

  • aster.cloud
  • April 13, 2023
  • 4 minute read

Use the for loop in Bash to create a handy color palette for the web.

When computer displays had a limited color palette, web designers often used a set of web-safe colors 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.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

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 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.

Read More  Artificial Intelligence, AI, Generative AI, Mercy, Azure OpenAI Service,

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 "&lt;div style='background-color:#$r$g$b'&gt;&lt;code&gt;#$r$g$b&lt;/code&gt;&lt;/div&gt;"
    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 &gt; websafe.html
(Jim Hall, CC BY-SA 4.0)

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&lt;&lt;EOF
&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;title&gt;Web-safe colors&lt;/title&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;

&lt;style&gt;
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;
}

}
  &lt;/style&gt;
&lt;/head&gt;
&lt;/body&gt;
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 "&lt;div
style='background-color:#$r$g$b'&gt;&lt;code&gt;#$r$g$b&lt;/code&gt;&lt;/div&gt;"

done
done
done

cat&lt;&lt;EOF

&lt;/body&gt;
&lt;/html&gt;
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 &gt; websafe.html
(Jim Hall, CC BY-SA 4.0)

By: Jim Hall
Originally published at Opensource


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Bash
  • Linux
  • Scripting
  • Tutorials
  • Web Development
You May Also Like
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • People
  • Technology

AI is automating our jobs – but values need to change if we are to be liberated by it

  • April 17, 2025
View Post
  • Software
  • Technology

Canonical Releases Ubuntu 25.04 Plucky Puffin

  • April 17, 2025
View Post
  • Computing
  • Public Cloud
  • Technology

United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services

  • April 15, 2025
View Post
  • Technology

Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology

  • April 2, 2025
View Post
  • Software
  • Technology

IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management

  • March 27, 2025
View Post
  • Technology

IBM contributes key open-source projects to Linux Foundation to advance AI community participation

  • March 22, 2025
View Post
  • Technology

Co-op mode: New partners driving the future of gaming with AI

  • March 22, 2025

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.