ファイル:HSV 2D profile of the rainbow gradient.png

提供: testwiki
ナビゲーションに移動 検索に移動
HSV_2D_profile_of_the_rainbow_gradient.png (640 × 480 ピクセル、ファイルサイズ: 20キロバイト、MIME タイプ: image/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."
日付
原典 投稿者自身による著作物
作者 Adam majewski
その他のバージョン
PNG 開発
InfoField
 この PNG ラスター画像C言語で作成されました。

ライセンス

この作品の著作権者である私は、この作品を以下のライセンスで提供します。
w:ja:クリエイティブ・コモンズ
表示 継承
このファイルはクリエイティブ・コモンズ 表示-継承 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
この画像は、ベクターイメージである SVG ファイルとして再作成されるべきです。これにはいくつかの利点があります。詳しくはCommons:Media for cleanupを参照してください。この画像の SVG 形式がすでに利用可能である場合は、アップロードしてください。アップロード後、この画像にあるこのテンプレートを{{Vector version available|新しい画像ファイル名.svg}}テンプレートで置き換えてください。

キャプション

このファイルの内容を1行で記述してください
HSV 2D profile of the rainbow gradient

このファイルに描写されている項目

題材

31 1 2020

ファイルの履歴

過去の版のファイルを表示するには、その版の日時をクリックしてください。

日時サムネイル寸法利用者コメント
現在の版2020年1月31日 (金) 17:58サムネイルなし640 × 480 (20キロバイト)wikimediacommons>Soul windsurferUser created page with UploadWizard

以下のページがこのファイルを使用しています: