// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace crudwork.Utilities
{
/// <summary>
/// Color Utility
/// </summary>
public static class ColorUtil
{
private static Random random = new Random();
private static int r;
private static int g;
private static int b;
/// <summary>
/// Return a randomly-generated color
/// </summary>
/// <returns></returns>
public static Color GetRandomColor()
{
r = random.Next(0, 256);
g = random.Next(0, 256);
b = random.Next(0, 256);
return Color.FromArgb(r, g, b);
}
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Color representation in h,s,v with h = [0 – 360], s,v = [0.0 – 1.0].
//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dotway.WPF.Controls.Utilities
{
///
///
public struct HSV
{
public HSV(double hue, double saturation, double value)
{
h = 0.0;
s = 0.0;
v = 0.0;
H = hue;
S = saturation;
V = value;
}
private double h;
public double H
{
get { return h; }
set
{
if (value >= 0 && value <= 360)
{
h = value;
}
else
{
throw new ArgumentOutOfRangeException("H is not in the span [0, 360].");
}
}
}
private double s;
public double S
{
get { return s; }
set
{
if (value >= 0.0 && value <= 1.0)
{
s = value;
}
else
{
throw new ArgumentOutOfRangeException("S is not in the span [0, 1].");
}
}
}
private double v;
public double V
{
get { return v; }
set
{
if (value >= 0.0 && value <= 1.0)
{
v = value;
}
else
{
throw new ArgumentOutOfRangeException("V is not in the span [0, 1].");
}
}
}
}
}
[/csharp]
Color representation in r,g,b with values [0.0 – 1.0].
//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.ComponentModel;
namespace Dotway.WPF.Controls.Utilities
{
///
///
public struct RGB
{
public RGB(Color color)
{
r = 0.0;
g = 0.0;
b = 0.0;
R = color.R / 255.0;
G = color.G / 255.0;
B = color.B / 255.0;
}
///
///
///
///
///
public RGB(byte red, byte green, byte blue)
{
r = 0.0;
g = 0.0;
b = 0.0;
R = red / 255.0;
G = green / 255.0;
B = blue / 255.0;
}
///
///
///
///
///
public RGB(double red, double green, double blue)
{
r = 0.0;
g = 0.0;
b = 0.0;
R = red;
G = green;
B = blue;
}
private double r;
public double R
{
get { return r; }
set
{
if (value >= 0.0 && value <= 1.0)
{
r = value;
}
else
{
throw new ArgumentOutOfRangeException("R is not in the span [0, 1]");
}
}
}
private double g;
public double G
{
get { return g; }
set
{
if (value >= 0.0 && value <= 1.0)
{
g = value;
}
else
{
throw new ArgumentOutOfRangeException("G is not in the span [0, 1]");
}
}
}
private double b;
public double B
{
get { return b; }
set
{
if (value >= 0.0 && value <= 1.0)
{
b = value;
}
else
{
throw new ArgumentOutOfRangeException("B is not in the span [0, 1]");
}
}
}
}
}
[/csharp]
Get all known color
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
ArrayList knownColorList = new ArrayList();
ArrayList knownColorNameList = new ArrayList();
public Form1() {
NonSystemColors(knownColorList, knownColorNameList);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void NonSystemColors(ArrayList knownColorList, ArrayList knownColorNameList) {
Array cA = Enum.GetValues(typeof(KnownColor));
foreach (KnownColor knwnC in cA) {
Color curC = Color.FromKnownColor(knwnC);
if (!curC.IsSystemColor) {
knownColorList.Add(curC);
knownColorNameList.Add(curC.Name.ToString());
}
}
}
protected override void OnPaint(PaintEventArgs pea) {
Graphics g = pea.Graphics;
int wi = 70, hi = 12, rectNb = 8;
int count = knownColorList.Count;
this.Width = (wi + 2) * rectNb + 9;
int y = (int)(count / rectNb);
this.Height = y * (2 + hi) + 60;
DisplayKnownColors(g, count, wi, hi, rectNb);
g.Dispose();
}
private void DisplayKnownColors(Graphics g, int count, int wi, int hi, int rectNb) {
Rectangle rec;
Pen p = new Pen(this.ForeColor);
Brush b;
StringFormat strfmt = new StringFormat();
strfmt.LineAlignment = strfmt.Alignment = StringAlignment.Near;
int x, y;
for (int i = 0; i < count; i++) {
x = (int)(i % rectNb);
y = (int)(i / rectNb);
rec = new Rectangle(1 + x * (2 + wi), 1 + y * (2 + hi), wi, hi);
g.DrawRectangle(p, rec);
b = new SolidBrush((Color)knownColorList[i]);
g.FillRectangle(b, rec);
b = new SolidBrush(Color.Black);
g.DrawString((string)knownColorNameList[i], this.Font, b, rec, strfmt);
}
}
}
[/csharp]
Use Color.FromArgb to create Color
using System;
using System.Drawing;
using System.Windows.Forms;
class RandomClear: Form
{
public static void Main()
{
Application.Run(new RandomClear());
}
public RandomClear()
{
Text = "Random Clear";
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
Random rand = new Random();
graphics.Clear(Color.FromArgb(rand.Next(256),
rand.Next(256),
rand.Next(256)));
}
}
Color.Chocolate
using System;
using System.Drawing;
using System.Windows.Forms;
class PaintEvent
{
public static void Main()
{
Form form = new Form();
form.Text = "Paint Event";
form.Paint += new PaintEventHandler(MyPaintHandler);
Application.Run(form);
}
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
graphics.Clear(Color.Chocolate);
}
}
Create two color instances with different alpha components
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Color c1 = Color.FromArgb(100, Color.Blue);
Color c2 = Color.FromArgb(50, Color.Green);
g.FillEllipse(Brushes.Red, 20, 20, 80, 80);
g.FillRectangle(new SolidBrush(c1), 60, 80, 60, 60);
Point[] pa = new Point[] {
new Point(150, 40),
new Point(90, 40),
new Point(90, 120)};
g.FillPolygon(new SolidBrush(c2), pa);
}
public static void Main() {
Application.Run(new Form1());
}
}