File:HSV 2D profile of the rainbow gradient.png

页面内容不支持其他语言。
這個文件來自維基共享資源
维基百科,自由的百科全书

HSV_2D_profile_of_the_rainbow_gradient.png(640 × 480像素,文件大小:20 KB,MIME类型:image/png


此图片可使用矢量图形重新创建为SVG文件。这具有很多好处;更多信息请参见共享资源:待清理媒体。如果存在此图片的SVG格式,请将其上传,然后将此模板替换为{{vector version available|新图片名称}}


建议您将SVG命名为“HSV 2D profile of the rainbow gradient.svg”,这样在使用Vector version available(或Vva)模板时就不需要填写新图片名称参数。

摘要

描述
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
其他版本

许可协议

我,本作品著作权人,特此采用以下许可协议发表本作品:
w:zh:知识共享
署名 相同方式共享
本文件采用知识共享署名-相同方式共享 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

说明

添加一行文字以描述该文件所表现的内容
HSV 2D profile of the rainbow gradient

此文件中描述的项目

描繪內容

HSV 英语

文件历史

点击某个日期/时间查看对应时刻的文件。

日期/时间缩⁠略⁠图大小用户备注
当前2020年1月31日 (五) 16:582020年1月31日 (五) 16:58版本的缩略图640 × 480(20 KB)Soul windsurferUser created page with UploadWizard

以下页面使用本文件:

全域文件用途

以下其他wiki使用此文件: