13
12

More than 5 years have passed since last update.

シェル上の文字列に色をつける(ピクセル画像も作る)(ANSI color code)

Last updated at Posted at 2014-09-16

文字に色をつける

Screenshot from 2014-09-16 16:15:26.png

ほとんどのシェルではシェル上で色をつけることを許容しているので、
ANSI escape codeという規格を通してやれば、様々な色の文字列を生成出来ます。
http://en.wikipedia.org/wiki/ANSI_escape_code

自分の環境でどのような色が使えるかは、

CentOS/RedHat系なら
/etc/DIR_COLORS
などを参照すればわかります。
http://www.atmarkit.co.jp/flinux/rensai/linuxtips/358dircolors.html

ちなみに僕の環境(CentOS)で/etc/DIR_COLORS
Screenshot from 2014-09-16 16:02:18.png
こんな感じでした。

読んでみると

/etc/DIR_COLORS
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

とのこと、これら

  • Attribute codes,
  • Text color codes,
  • Background color codes,

を組み合わせて文字の色・背景・装飾をいじるみたいです。

echo "\e"もしくはecho "\033"
で生成するエスケープ文字を(escape)として(ブラウザで表示できない)

(escape)[(上記の数字);(上記の数字)m (texts..)
などという風に上記の00~08,30~37,40~47の数字を;で区切って(escape)[mの間に挟めば
その地点より文字の表示の仕方が変更されます。
もとに戻すときは(escape)[0mとすれば十分です。

なお、属性はあとから上書きされていくため、Screenshot from 2014-09-16 16:19:57.png
こんな風になります。

このサイトを参考にしました。
http://www.linuxquestions.org/questions/linux-software-2/adding-colors-to-your-motd-105038/

シェル上で表示可能な画像

これを応用すると、ログイン時やプログラム実行時に使えるピクセル画像を作れます

シェルで表示させたとき
Screenshot from 2014-09-16 15:46:14.png

テキストエディタで開いたとき
Screenshot from 2014-09-16 15:53:21.png

AAABBCC
AABBBBD
AAADDDD

のような書き方から上のマリオの画像のようなものを生成
13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12