• jinusa2007@gmail.com
  • 1-646-580-5887

color combination and create new color from two given color in .net

     -

color combination and create new color from two given color in .net

Lets discuss color capabilities in .net

Github link

For most people Color is RGB or ARGB

In.net

Color is a struct. This type provides a standard way to specify and mutate colors in the C# language. By adding a reference to the System.Drawing assembly, you can access this type and avoid writing your own color routines.

Colors can be represented as ARGB values, which store the alpha transparency, as well as the red, green and blue values. These values are stored as bytes which gives them a range of 0 to 255 inclusive.

So in order to make new color ( thats what this code does) suppose you have two colors color a and color b

Step 1

Please take the color a and  convert into RGB component via  Private Class colorcomponent

Step 2

do the same step with color b

now for individual R  take the average or fraction s if you are creating more than 2 color

Step 3

suppose R component is 100 for color a

suppose R component is 150 for color b

Step 4

then the middle color should have R value as 125

you may create 4 intermediate color in  that case  your will have R  as 110 120 130 140

Step 5 do same for G a nd B also and combine them to color back see colorcomponent class

2016-10-10_11-50-30

color-combination-demo-in-net

please download zip code file

Asp.net code

<p> How to use this class “colorcomponent” </p>
Dim colora As Color
colora = Color.Red
Dim colorb As Color
colorb = Color.Blue
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolor = 4
Dim outColor(numberofcolor + 1) As Color
ReDim outColor(numberofcolorinbetween + 1)
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolorinbetween + 1)

vb.net code for desktop application

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb))
ListBox1.Items.Add(ColorTranslator.ToHtml(colora))

End Sub
Private Class colorcomponent
Dim r As Integer
Dim g As Integer
Dim b As Integer
Dim a As Integer

Public Sub setcolor(C As Color)
Me.r = C.R
Me.g = C.G
Me.b = C.B
End Sub

Public Function getcolor(colorcomponent1 As colorcomponent) As Color
getcolor = Color.FromArgb(colorcomponent1.r Mod 256, colorcomponent1.g Mod 256, colorcomponent1.b Mod 256)

End Function
Public Function getcolorwithalpha(colorcomponent1 As colorcomponent, alpha As Byte) As Color
getcolorwithalpha = Color.FromArgb(alpha, CByte(colorcomponent1.r Mod 256), CByte(colorcomponent1.g Mod 256), CByte(colorcomponent1.b Mod 256))

End Function

Public Function getcolorarray(colorcomponent1 As colorcomponent, colorcomponent2 As colorcomponent, numberofcolorsneededinbetween As Double) As Color()

Dim outcolorcomponent As New colorcomponent()
Dim outColor(numberofcolorsneededinbetween) As Color
outColor(0) = getcolor(colorcomponent1)
If numberofcolorsneededinbetween > 0 Then
For i As Double = 1 To numberofcolorsneededinbetween – 1
If (colorcomponent1.r – colorcomponent2.r <> 0) Then
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.r = (colorcomponent1.r)
End If
If (colorcomponent1.g – colorcomponent2.g <> 0) Then
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.g = (colorcomponent1.g)
End If
If (colorcomponent1.b – colorcomponent2.b <> 0) Then
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.b = (colorcomponent1.b)
End If
outColor(i) = getcolor(outcolorcomponent)
Next
End If
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2)

Return outColor
End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolors = 4
Dim outColor(numberofcolors + 1) As Color
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1)
ListBox1.Items.Clear()
Dim i = 1
For Each genfcolor As Color In outColor
Dim t As New TextBox()

Dim j = 100
‘ t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor
t.Text = ColorTranslator.ToHtml(genfcolor)
‘Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor))
‘ t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
Me.Controls.Add(t)
t.Location = New Point(8 * 50, 4 + i * 30)
Me.Show()
i += 2
Next

End Sub

End Class

c# code
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
private void Form1_Load(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb));
ListBox1.Items.Add(ColorTranslator.ToHtml(colora));

}
private class colorcomponent
{
int r;
int g;
int b;

int a;
public void setcolor(Color C)
{
this.r = C.R;
this.g = C.G;
this.b = C.B;
}

public Color getcolor(colorcomponent colorcomponent1)
{
return Color.FromArgb(colorcomponent1.r % 256, colorcomponent1.g % 256, colorcomponent1.b % 256);

}
public Color getcolorwithalpha(colorcomponent colorcomponent1, byte alpha)
{
return Color.FromArgb(alpha, Convert.ToByte(colorcomponent1.r % 256), Convert.ToByte(colorcomponent1.g % 256), Convert.ToByte(colorcomponent1.b % 256));

}

public Color[] getcolorarray(colorcomponent colorcomponent1, colorcomponent colorcomponent2, double numberofcolorsneededinbetween)
{

colorcomponent outcolorcomponent = new colorcomponent();
Color[] outColor = new Color[numberofcolorsneededinbetween + 1];
outColor(0) = getcolor(colorcomponent1);
if (numberofcolorsneededinbetween > 0) {
for (double i = 1; i <= numberofcolorsneededinbetween – 1; i++) {
if ((colorcomponent1.r – colorcomponent2.r != 0)) {
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.r = (colorcomponent1.r);
}
if ((colorcomponent1.g – colorcomponent2.g != 0)) {
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.g = (colorcomponent1.g);
}
if ((colorcomponent1.b – colorcomponent2.b != 0)) {
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.b = (colorcomponent1.b);
}
outColor(i) = getcolor(outcolorcomponent);
}
}
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2);

return outColor;
}
}

private void Button1_Click(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
colorcomponent coloracomponent = new colorcomponent();
colorcomponent colorbcomponent = new colorcomponent();
coloracomponent.setcolor(colora);
colorbcomponent.setcolor(colorb);
dynamic numberofcolors = 4;
Color[] outColor = new Color[numberofcolors + 2];
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1);
ListBox1.Items.Clear();
dynamic i = 1;
foreach (Color genfcolor in outColor) {
TextBox t = new TextBox();

dynamic j = 100;
// t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor;
t.Text = ColorTranslator.ToHtml(genfcolor);
//Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor));
// t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
this.Controls.Add(t);
t.Location = new Point(8 * 50, 4 + i * 30);
this.Show();
i += 2;
}

}
public Form1()
{
Load += Form1_Load;
}

}

 

Archives