Calculating intensity for an RGB Color.

huangapple go评论66阅读模式
英文:

Calculating intensity for an RGB Color

问题

I would like to ask how I can calculate my variable y for an RGB color intensity. These r, g, b letters in my RGB_Trypel struct are representing each array element for the arrays red, yellow, green, white, black. So I need to multiply these r, g, b array elements with the displayed numbers in my y variable.

I would appreciate some help.

#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>;

struct RGB_Trypel
{
	unsigned char r, g, b; // stores values only up to 255 and positive values
};

int main(void)
{
	struct RGB_Trypel red = { 255, 0, 0 }, yellow = { 255, 255, 0 }, green = { 0, 255, 0 };
	const struct RGB_Trypel white = { 255, 255, 255 };
	const struct RGB_Trypel black = { 0, 0, 0 };
	struct RGB_Trypel colorArray[5] = { red, yellow, green, white, black };
	int n = 5;

	unsigned char intensity(struct RGB_Trypel t)
	{
		int y;
		y = 0.299 * t.r + 0.587 * t.g + 0.114 * t.b;
		return y;
	}

	for (int i = 0; i < n; i++)
	{
		printf("(%3d, %3d, %3d): \t intensity(%d) \n", colorArray[i].r, colorArray[i].g, colorArray[i].b, intensity(colorArray[i]));
	}

	return 0;
}

Note: I've corrected some syntax issues in your code.

英文:

i would like to ask how i can calculate my variable y for an RGB color intensity.

These r,g,b letters in my RGB_Trypel struct are representing each array element for the arrays rot, gelb, gruen, weiss, schwarz. So i need here to multiply these r,g,b array elements with the shown numbers in my y variable.

i would really be glad for some help.




#define CRT_SECURE_NO_WARNINGS
#include &lt;stdio.h&gt;

struct RGB_Trypel
	{
		
	unsigned char r, g, b; // speichert Werte nur bis 255 und positive Werte

	};




int main(void)
{
	
	struct RGB_Trypel rot = { 255,0,0 }, gelb = { 255,255,0 }, gruen = { 0,255,0 };

	const  struct RGB_Trypel weiss = { 255,255,255 };
	const  struct RGB_Trypel schwarz = { 0,0,0 };

	struct RGB_Trypel fafeld[5] = { rot, gelb, gruen, weiss, schwarz };
	int n = 5;
	
	unsigned char intensiteat(struct RGB_Trypel t);
	{
		int = y;
		

		y = 0.299 * r + 0.587 * g + 0.114 * b;
	}
	


	for (int i = 0; i &lt; n; i++)
	{

		printf(&quot;(%3d,%3d,%3d): \t intensiteat(%d) \n&quot;,fafeld[i].r, fafeld[i].g, fafeld[i].b, intensiteat(fafeld[i]);


	}


	return 0;
}

I am not really sure how i can put my struct array element into a function and let it multiply following numbers in my y variable.

答案1

得分: 1

unsigned char intensiteat(struct RGB_Trypel t)
{
int y;
y = 0.299 * t.r + 0.587 * t.g + 0.114 * t.bb;
}

You have other errors, but this should help you get started.

BTW, you missed an underscore at the beginning of that define. It should be:

#define _CRT_SECURE_NO_WARNINGS

英文:

Your big problem here is that this

 unsigned char intensiteat(struct RGB_Trypel t);
{
    int = y;
    

    y = 0.299 * r + 0.587 * g + 0.114 * b;
}

looks like an attempt to define a function inside another function. This wont work. Move it to be before main. Like this:

 unsigned char intensiteat(struct RGB_Trypel t)
{
    int = y;
    

    y = 0.299 * r + 0.587 * g + 0.114 * b;
}


 int main(void)
 {

second, to refer to fields of a struct inside that function do this:

 unsigned char intensiteat(struct RGB_Trypel t)
{
    int = y;
    

    y = 0.299 * t.r + 0.587 * t.g + 0.114 * t.bb;
}

you have multiple other errors, but this will get you started

BTW you missed an _ of the front the that define. Should be:

#define _CRT_SECURE_NO_WARNINGS

huangapple
  • 本文由 发表于 2023年3月31日 01:57:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定