File:HSV 2D profile of the rainbow gradient.png
頁面內容不支援其他語言。
外觀
HSV_2D_profile_of_the_rainbow_gradient.png (640 × 480 像素,檔案大小:20 KB,MIME 類型:image/png)
本圖片是以PNG、GIF或JPEG格式上傳。然而,其中包含的資料或訊息,應該重新建立成可縮放向量圖形(SVG)檔案,以更有效率或更準確的方式儲存。如有可能,請上傳本圖片的SVG格式版本。在上傳之後,請修改維基各姊妹計畫中所有使用舊版圖片的條目(列在圖像連結章節中),替換為新版圖片,並在舊圖片的描述頁中加入{{Vector version available|新圖片名稱.svg}}模板,同時移除本模板。
|
摘要
描述HSV 2D profile of the rainbow gradient.png |
English: HSV 2D profile of the rainbow gradient : "cyclic colormap traversing HSV color space. The map is obtained by linearly varying the hue through all possible values while keeping constant maximum saturation and value." |
日期 | |
來源 | own work. I use the code from gnuplot by Petr Mikulik |
作者 | Adam majewski |
其他版本 |
|
授權條款
我,本作品的著作權持有者,決定用以下授權條款發佈本作品:
此檔案採用共享創意 姓名標示-相同方式分享 4.0 國際授權條款。
- 您可以自由:
- 分享 – 複製、發佈和傳播本作品
- 重新修改 – 創作演繹作品
- 惟需遵照下列條件:
- 姓名標示 – 您必須指名出正確的製作者,和提供授權條款的連結,以及表示是否有對內容上做出變更。您可以用任何合理的方式來行動,但不得以任何方式表明授權條款是對您許可或是由您所使用。
- 相同方式分享 – 如果您利用本素材進行再混合、轉換或創作,您必須基於如同原先的相同或兼容的條款,來分布您的貢獻成品。
c source code
/*
https://gitlab.com/adammajewski/color_gradient
c console program for testing color gradient
uses a code from gnuplot
gcc h.c -lm -Wall
a@zelman:~/c/varia/outor/gradient/hsv$ ./a.out
===========================
h= 360.000000 s=0.000000 v=1.000000
r= 1.000000 g=1.000000 b=1.000000
-----------------------------------
h= 0.000000 s=1.000000 v=1.000000
r= 1.000000 g=0.000000 b=0.000000
*/
#include <stdio.h>
#include <string.h> // strncat
#include <stdlib.h> // malloc
#include <math.h> // floor; needs -lm also
// size of virtual 2D array of pixels
// each piexel has a RGB color
int iWidth = 600;
int iHeight ; //
// size of the dynamic 1D array
unsigned char * data;
size_t ColorSize = 3; // RGB = number of color components = channels
size_t ArrayLength; // number of 1D array's elements = ENumber = iWidth*iHeight*ColorSize
size_t ElementSize; // size of array's element in bytes
size_t ArraySize; // size of array in bytes = ElementSize*ArrayLength
// ppm P6 file
size_t HeaderSize ; // size of the P6 file header in bytes
size_t FileSize; // = HeaderSize +ArraySize [bytes]
// gives position of 2D point (ix,iy) in 1D array ; uses also global variables: iWidth , ColorSize
int Give_i ( int iX, int iY)
{
return (iX + iY * iWidth) * ColorSize;
}
/*
GNUPLOT : http://gnuplot.sourceforge.net/
file : getoutor.c
* Petr Mikulik, December 1998 -- June 1999
* Copyright: open source as much as possible
*
*/
// from colour.h
/* Contains a colour in RGB scheme.
Values of r, g and b are all in range [0;1] */
typedef struct {
double r, g, b;
} rgb;
/* hsv :
h : deg [0,360]
s : % [0.0,100.0]
v : % [0.0,100]
*/
typedef struct {
double h, s, v;
} hsv;
/*
* outor Conversion Algorithms
* taken from http://www.cs.rit.edu/~ncs/outor/t_convert.html
* each outor model should provide a conversion to RGB.
* RGB values are clipped to [0,1] as some outors in some
* models have no RGB value.
*/
static void
HSV_2_RGB(hsv in, rgb *out)
{
double h,s,v, f,p,q,t;
int i;
h = in.h;
s = in.s;
v = in.v;
if (s == 0) { /* achromatic (gray) */
out->r = out->g = out->b = v;
return;
}
h *= 6.; /* h range in gnuplot is [0,1] and not the usual [0,360] */
i = floor(h);
f = h - i;
p = v * (1.0 - s);
q = v * (1.0 - s*f);
t = v * (1.0 - s*(1.0-f));
switch(i % 6) {
case 0:
out->r = v;
out->g = t;
out->b = p;
break;
case 1:
out->r = q;
out->g = v;
out->b = p;
break;
case 2:
out->r = p;
out->g = v;
out->b = t;
break;
case 3:
out->r = p;
out->g = q;
out->b = v;
break;
case 4:
out->r = t;
out->g = p;
out->b = v;
break;
default:
out->r = v;
out->g = p;
out->b = q;
break;
}
}
void PrintColor( FILE *fp, double position, hsv in, rgb out){
// normalized to [0.0, 1.0] range RGB color channels
double R = out.r;
double G = out.g;
double B = out.b;
// [Relative luminance is formed as a weighted sum of linear RGB components](https://en.wikipedia.org/wiki/Luma_(video))
//
//from function test_palette_subcommand from file gnuplot/src/command.c test_palette_subcommand
//ntsc = 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b;
double Y = 0.299*R + 0.587*G + 0.114*B;
//print
fprintf(fp, "%f\t", position);
fprintf(fp, "%f\t %f\t%f\t%f\t", R, G, B, Y);
fprintf(fp, "%f\t %f\t %f\n", in.h, in.s, in.v);
}
/* position -> hsv -> rgb
input position double [0.0, 1.0]
output rgb = unsigned char c array
rainbow gradient = Rainbow colormap or sawtooth rainbow or hsv
*/
void ComputeAndPrintColor( FILE *fp, double position, unsigned char c[]){
// "cyclic colormap traversing HSV color space. The map is obtained by linearly varying the hue through all possible values while keeping constant maximum saturation and value."
hsv in = {position, 1.0, 1.0}; // set palette model HSV functions gray,1,1
rgb out;
// compute hsv from position
HSV_2_RGB(in, &out); // convert hsv to rgb
//
PrintColor( fp, position, in, out);
// change range
c[0] = 255*out.r; //R
c[1] = 255*out.g; // G
c[2] = 255*out.b; // B
}
int PlotPoint(unsigned char A[], int i, unsigned char color[]){
A[i] = color[0]; /* Red*/
A[i+1] = color[1]; /* Green */
A[i+2] = color[2]; /* Blue */
return 0;
}
// --------------------
int FillArray (unsigned char A[] , int n){
int iX;
int iXmax = iWidth;
int iY;
int iYmax = iHeight;
int i; // index of 1D array
double position; // number form 0.0 to 1.0 used for color gradient
unsigned char color[3]; //
// text file used by the gnuplot for creating images
char name [100]; /* name of file */
snprintf(name, sizeof name, "%d", n); /* */
char *filename =strncat(name,".txt", 4);
FILE *fp = fopen(filename, "w");
if (fp==NULL) {
printf("Error opening text file!\n");
return 1;
}
fprintf(fp, "# position \t\t R \t\t G \t\tB \t\tY \t h\ts\tv\n"); // header of the text file
//
for(iX=0; iX<iXmax; ++iX){
position = (double) iX / iXmax;
ComputeAndPrintColor(fp, position, color);
// vertical
for(iY=0; iY<iYmax; ++iY){
i = Give_i(iX, iY);
PlotPoint(A, i , color);
}
}
fclose(fp);
return 0;
}
// --------------- save dynamic "A" array of uinsigned char to the binary ppm file ( P6 ) --------------------------------
int SaveArray2PPM (unsigned char A[], size_t ASize, int k)
{
FILE *fp;
const unsigned char MaxColorComponentValue = 255; /* color component is coded from 0 to 255 ; it is 8 bit color file */
char name [100]; /* name of file */
snprintf(name, sizeof name, "%d", k); /* */
char *filename =strncat(name,".ppm", 4);
/* save image to the pgm file */
fp = fopen (filename, "wb"); /*create new file,give it a name and open it in binary mode */
if (fp == NULL)
{ printf("File open error");
return 1;}
else {
fprintf (fp, "P6\n%u %u\n%u\n", iWidth, iHeight, MaxColorComponentValue); /*write header to the file */
fwrite (A, ASize, 1, fp); // write dynamic A array to the binary file in one step
printf ("File %s saved. \n", filename);
fclose (fp);
return 0;}
}
// n = nummber of the gradient function
int MakeGradientImage(unsigned char A[], int n){
FillArray(data, n);
SaveArray2PPM(data, ArraySize, n+iWidth);
return 0;
}
int setup(){
iHeight = iWidth/3;
// 1D array
ArrayLength = iWidth*iHeight*ColorSize;
ElementSize = sizeof(unsigned char);
ArraySize = ElementSize*ArrayLength ;
HeaderSize = 11 + (size_t) (log10(iHeight) +log10(iWidth));
FileSize = HeaderSize + ArraySize;
/* create dynamic 1D array for RGB colors */
data = malloc (ArraySize);
if (data == NULL ){
printf ( "Could not allocate memory for the array\n");
return 1;}
return 0;
}
void info(){
printf("ppm (P6) header size = %zu bytes\n", HeaderSize);
printf("Array Size = %zu bytes\n", ArraySize);
printf("PPM file size = %zu bytes\n", FileSize);
}
int end(){
printf (" allways free memory (deallocate ) to avoid memory leaks \n"); // https://en.wikipedia.org/wiki/C_dynamic_memory_allocation
free (data);
info();
return 0;
}
int main(){
setup();
MakeGradientImage(data, 0);
end();
return 0;
}
Gnuplot src code
# save as a plot2.gp
# run : gnuplot plot2.gp
set palette model HSV functions gray,1,1
set cbrange [0:1]
# Set the output file type
set terminal png
# list of gradient names; update it maually
# gnuplot array is numbered from 1 to words(array), not like c arrays
# update list with order as in function GiveColor from p.c
titles = "Rainbow Linas Magma GrayL GrayNL2 GrayNL3 GraySqrt Green NewLinas CoolWarm GrayGamma GrayNL3Wave2 GrayNL3Wave10 GraySqrtWave ColorLWave ColorLWaveInverted NL3Wave5NonInv"
spaces = "RGB HSV"
# length of array spaces = nMax, but tex files are numbered from 0 to nMax-1 ( c style)
nMax = words(spaces)
# legend
set key inside bottom center horizontal
set key horizontal
set key font ",8"
# remove upper and right axis
set border 3 back
set xtics nomirror out
set ytics nomirror
set xlabel "gradient position"
set ylabel " intensity"
# adjust y range to make a space for the legend
set yrange [-0.2:1.1]
n=0
# Set the output file name
outfile = sprintf('%d.png',n)
set output outfile
# Set the intput file name
infile = sprintf('%d.txt',0)
# title of the image for the array of strings
sTitle = sprintf(" %s profiles of the rainbow colormap", word(spaces,n) )
set title sTitle
# Now plot the data with lines and points
plot infile using 1:2 with lines linecolor rgb 'red' title 'R', \
'' using 1:3 w lines linecolor rgb 'green' title 'G', \
'' using 1:4 w lines linecolor rgb 'blue' title 'B',\
'' using 1:5 w lines linecolor rgb 'black' title 'Y'
# Set the output file name
outfile = sprintf('%d_hsv.png',n)
set output outfile
# Set the intput file name
infile = sprintf('%d.txt',0)
# title of the image for the array of strings
sTitle = sprintf(" %s profiles of the rainbow colormap", word(spaces,n) )
set title sTitle
# Now plot the data with lines and points
plot infile using 1:6 with lines linecolor rgb 'red' title 'H', \
'' using 1:7 w lines linecolor rgb 'green' title 'S', \
'' using 1:8 w lines linecolor rgb 'blue' title 'V'
# 3d plot of rgb profile
set xlabel "R"
set ylabel "G"
set zlabel "B"
unset yrange
set nokey
unset tics
set xrange [0.0:1.0]
set yrange [0.0:1.0]
set zrange [0.0:1.0]
set grid
set border 4095 ls 1 lc rgb "black" # Draw a complete box around a `splot`:
set xzeroaxis
set yzeroaxis
# set view <rot_x>{,{<rot_z>}{,{<scale>}{,<scale_z>}}}
# view is 60 rot_x, 30 rot_z, 1 scale, 1 scale_z
set view 60,75
set view equal xyz # to get an orthographic projection with all of the axes scaled equally
set xyplane at 0 # adjusts the position at which the xy plane is drawn in a 3D plot
# Set the output file name
outfile = sprintf('%d_3d_60_75_v.png',n)
set output outfile
# title of the image for the array of strings
sTitle = sprintf("3D %s profile of the rainbow colormap", word(spaces,n+1) )
set title sTitle
# http://www.bersch.net/gnuplot-doc/linetypes,-colors,-and-styles.html
# 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.
# here rgb value is computed
rgb(r,g,b) = 65536*int(255*r) + 256*int(255*g) + int(255*b)
# http://gnuplot-surprising.blogspot.com/2012/10/
#convert dec color value to its hex string
dec2hex(dec)=gprintf("%02x",dec)
#convert rgb color value to its hex string
rgb2hex(r,g,b)="#".\
dec2hex(255*r).\
dec2hex(255*g).\
dec2hex(255*b)
# Now plot the data with lines and points
set label "(0,0,0)" at -0.1,0,0 right
#set label "0" at 1,-0.1,0 right
#set label "1" at 1,+1.1,0 right
#set label "2" at 0,1.1,0 right
#set label "3" at 0,1,1 left
#set label "4" at 0,0,1 left
# set label "5" at 1,0,1 left
#set label "6" at 1,0,0 left
set label "(1,1,1)" at 1.1,1,1 left
splot infile using 1:1:1 with lines lw 3 lc rgb 'gray',\
infile using 2:3:4:(rgb($2,$3,$4)) with points pt 7 ps 2 lc rgb variable ,\
'-' w p pt 7 ps 2 lc rgb 'black'
0 0 0
1 1 1
e
沒有維基數據項目的某些值
31 1 2020
多媒體型式 繁體中文 (已轉換拼寫)
image/png
檔案歷史
點選日期/時間以檢視該時間的檔案版本。
日期/時間 | 縮圖 | 尺寸 | 用戶 | 備註 | |
---|---|---|---|---|---|
目前 | 2020年1月31日 (五) 16:58 | 640 × 480(20 KB) | Soul windsurfer | User created page with UploadWizard |
檔案用途
下列頁面有用到此檔案:
全域檔案使用狀況
以下其他 wiki 使用了這個檔案:
- en.wikipedia.org 的使用狀況
- en.wikibooks.org 的使用狀況
- ja.wikipedia.org 的使用狀況
- pt.wikipedia.org 的使用狀況
隱藏分類: