← Back to C Course | Chapter 13: Graphics | Lesson 5 of 10

C Graphics में Colors

Colors आपको अपनी lines, shapes, और backgrounds के लिए paint चुनने देते हैं, crayons चुनने जैसा। आप areas को भी एक color से भर सकते हैं।
Syntax
c
setcolor(COLOR);        // foreground color for lines and outlines
setbkcolor(COLOR);      // background color of the window
shape(x, y, ...);       // drawn using the current color

Standard Color Constants

BGI graphics library 0 से 15 तक numbered 16 standard color constants define करती है, BLACK, BLUE, GREEN, RED, YELLOW, और WHITE जैसे common colors cover करते हुए, आपको raw RGB values specify करने की ज़रूरत के बिना draw करने के लिए एक fixed, portable palette देते हुए।

उदाहरण: Standard Color Constants

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setcolor(RED);
	circle(100, 100, 30);
	getch();
	closegraph();
	return 0;
}

Foreground Color Set करना

setcolor() lines और shape outlines के लिए इस्तेमाल होने वाला active drawing color उस point से program में आगे बदल देता है; call के बाद draw हुई हर line() या outline shape नया color इस्तेमाल करती है, जबकि पहले draw हुई कोई भी चीज़ जो भी color draw होते समय active था वह रखती है।

उदाहरण: Setting Foreground Color

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setcolor(GREEN);
	line(10, 10, 100, 100);
	getch();
	closegraph();
	return 0;
}

Background Color Set करना

setbkcolor() background color बदलता है जो आपकी drawings के पीछे पूरी graphics window भरता है, setcolor() से अलग जो सिर्फ बाद में draw किए shapes के outline color को affect करता है — background बदलना पूरे canvas को एक साथ recolor करता है।

उदाहरण: Setting Background Color

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setbkcolor(BLUE);
	cleardevice();
	getch();
	closegraph();
	return 0;
}

Fill Styles और Colors

setfillstyle() control करता है कि closed shapes के interior को कैसे भरा जाए, एक pattern constant (जैसे flat color के लिए SOLID_FILL या crosshatch pattern के लिए HATCH_FILL) को उस color के साथ लेते हुए जिससे भरना है, और बाद में draw किए किसी भी filled shape पर apply होता है।

उदाहरण: Fill Styles and Colors

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setfillstyle(SOLID_FILL, RED);
	bar(50, 50, 150, 150);
	getch();
	closegraph();
	return 0;
}

Shapes को Flood Fill करना

floodfill() एक पूरे enclosed region को current fill style और color से भरता है, आपके उस region के अंदर दिए एक seed coordinate से शुरू करते हुए और specified border color से match करने वाले pixels hit करने तक बाहर फैलते हुए, एक paint bucket tool जैसा।

उदाहरण: Flood Filling Shapes

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	circle(100, 100, 50);
	setfillstyle(SOLID_FILL, YELLOW);
	floodfill(100, 100, WHITE);
	getch();
	closegraph();
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. shape draw करने के बाद color set करना, ताकि यह उस shape को affect न करे।
  2. setcolor (outline और text) को setfillstyle (fill) से confuse करना, ताकि fill color न बदले।
  3. एक color constant नाम इस्तेमाल करना जो graphics.h में defined नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.