Showing posts with label rgb color. Show all posts
Showing posts with label rgb color. Show all posts

Wednesday, October 31, 2012

Convertting a rgb color value to its hex-string

In a previous post, I have talked about how to convert a hex-string color to its rgbvalue. This time I will talk about the inversion, i.e., converting a rgb value color specification to its hex-string correspondence.

#pick the red color value out
red(rgbvalue)=rgbvalue/65536
#pick the green color value out
green(rgbvalue)=(rgbvalue%65536)/256
#pick the blue color value out
blue(rgbvalue)=rgbvalue%256
#convert dec color value to its hex string
dec2hex(dec)=gprintf("%02x",dec)
#convert rgb color value to its hex string
rgb2hex(color)="#".\
               dec2hex(red(color)).\
               dec2hex(green(color)).\
               dec2hex(blue(color))

color = 65535 #rgb-value 
print "The hex string of rgb-color ". color ." is: "
print rgb2hex(color)
plot cos(x) w line linecolor rgb rgb2hex(color)

Run this script, it will output the following string and picture.

The hex string of rgb color 65535 is: 
#00ffff

Fig.1 Converting from a rgb color specification to its hex-string


Friday, August 24, 2012

Converting from a hex-string color to its rgbvalue

There are several ways to specify color when one plots using gnuplot.Among them hex-string (like "#0000ff") and rgbvalue (like "256") specification is very important. In this post we will talk about converting from a hex-string color to its rgbvalue (the inversion will be talked in another post).

reset
#pick the red color hex string out
red(colorstring)= colorstring[2:3]
#pick the green color hex string out
green(colorstring)=colorstring[4:5]
#pick the blue color hex string out
blue(colorstring)=colorstring[6:7]
#convert a hex string to its dec format
hex2dec(hex)=gprintf("%0.f",int('0X'.hex))
#calculate the rgb value from the r,g,b weight
rgb(r,g,b) = 65536*int(r)+256*int(g)+int(b)
#convert the hex color string to its rgb value.
hex2rgbvalue(color)=rgb(\
                        hex2dec(red(color)),\
                        hex2dec(green(color)),\
                        hex2dec(blue(color))\
                        ) 
set term png
set output "hex_string2rgbvalue.png"
plot 'color.dat' u 1:2:(hex2rgbvalue(stringcolumn(3)))\
              w lp pt 7 ps 3 lc rgb variable notitle
set output

File 'color.dat' has content like the following:
0 1 #ff0000
1 2 #00ff00
2 3 #0000ff
3 4 #ff00ff
4 5 #ffff00
5 6 #00ffff
0 6 #00ffff
1 5 #ffff00
2 4 #ff00ff
3 3 #0000ff
4 2 #00ff00
5 1 #ff0000

The output file 'hex_string2rgbvalue.png' looks like this:
Fig.1 Converting from a hex-string color to its rgb-value

Tuesday, September 27, 2011

Convert a rgb colorized png picture to a gray one using gnuplot

I will talk about convert a colorized rgb image to a gray one using gnuplot in this article.
Amazing it is to edit pictures with gnuplot. Yes, it is a little amazing, this kind of works should left to picture editors in fact. But I will show you gnuplot can also do it well.
In a rgb image, the color of a point is marked with (r,g,b) where r,g and b can be and always are diffrent values. While for a gray one r,g and b must be the same. So to convert a rgb image to a gray one, we should convert (r,g,b) to (gray,gray,gray). Once we known how to do it, the problem becomes easy. Now we come to write our gnuplot script.
#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rgbtogray.gnuplot" inputfile outputfile
#parameters:
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
inputfile="$0.png"
outputfile="$1.png"
plot inputfile binary filetype=png w rgbimage
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax]
set yrange [ymin:ymax]
#get the size of the picture
gray(r,g,b)=0.299*r + 0.587*g + 0.114*b
#The function which convert rgb color to gray.
#At first I choose the three weighting all to be 0.333,
#but a firend told me it will be better to use these values
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
#########################
#There should not be any margin in the output picture
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
unset key   #The key, border and tics are all do not need
unset border
unset tics
#set terminal and output file name
set term png size (xmax-xmin),(ymax-ymin)
set output outputfile
#Plot with gray color
plot inputfile\
     u (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3)))\
     binary filetype=png w rgbimage
Save the script as "rgbtogray.gnuplot", then it can be called like this:
gnuplot> call "rgbtogray.gnuplot" inputfile outputfile
The following is two groups of our input and ouput files. The initial input files are copied from here and here (Thanks the original picture author!).
 

Input file-1

Output file-1

Input file-2

Output file-2
Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.