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

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



please download zip code file 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()
Dim numberofcolor = 4
Dim outColor(numberofcolor + 1) As Color
ReDim outColor(numberofcolorinbetween + 1)
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolorinbetween + 1) 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

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)))
outcolorcomponent.r = (colorcomponent1.r)
End If
If (colorcomponent1.g – colorcomponent2.g <> 0) Then
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)))
outcolorcomponent.g = (colorcomponent1.g)
End If
If (colorcomponent1.b – colorcomponent2.b <> 0) Then
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)))
outcolorcomponent.b = (colorcomponent1.b)
End If
outColor(i) = getcolor(outcolorcomponent)
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()
Dim numberofcolors = 4
Dim outColor(numberofcolors + 1) As Color
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1)
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)
‘ t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
t.Location = New Point(8 * 50, 4 + i * 30)
i += 2

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;

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();
dynamic numberofcolors = 4;
Color[] outColor = new Color[numberofcolors + 2];
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1);
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);
// t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
t.Location = new Point(8 * 50, 4 + i * 30);
i += 2;

public Form1()
Load += Form1_Load;



Free ebook: Introducing Microsoft SQL Server 2016


how to see where the log is ? Logger in slf4j,

Logger in slf4j, could you explain and give explain on how to use it? (e.g. how to see where the log is)



So  Like any  logger library  we have a configuration file where we place the locationof logs  it can be DB text file Console out put  DEbugger on statments




Logger in slf4j, could you explain and give explain on how to use it? (e.g. how to see where the log is)


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

private final Logger log = LoggerFactory.getLogger(getClass());;



Using slf4j with Simple logger

Create a Maven based project and this in your pom.xml.






Now you may use Logger in your Java code like this.

package deng;

import org.slf4j.*;

public class Hello {

static Logger LOGGER = LoggerFactory.getLogger(Hello.class);

public static void main(String[] args) {

for (int i = 0; i < 10; i++)

if (i % 2 == 0)“Hello {}”, i);


LOGGER.debug(“I am on index {}”, i);



The above will get your program compiled, but when you run it, you will see these output.

bash> java deng.Hello

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See for further details.

What it’s saying is that at runtime, you are missing the logging “implementation” (or the logger binding), so slf4j simply use a “NOP” implmentation, which does nothing. In order to see the output properly, you may try use an simple implementation that does not require any configuration at all! Just go back to your pom.xml and add the following:






Now you see logging output on STDOUT with INFO level. This simple logger will default show any INFO level message or higher. In order to see DEBUG messages, you would need to pass in this System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup.

Using slf4j with Log4j logger

Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is to replace slf4j-simple with another popular logger implementation, such as the Log4j.






Again, we must configure logging per implementation that we picked. In this case, we need an

src/main/resources/ file.

log4j.rootLogger=DEBUG, STDOUT




log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) – %m%n

src/main/resources/ file for  writing into file  and stdout

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




Re-run your program, and you should see similar output.

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.






Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as src/main/resources/, but you would also need to add a System properties in order to have it pick it up. Here is an example to get you started:






Using slf4j with Logback logger

The logback logger implementation is a super dupa quality implementation. If you intend to write serious code that go into production, you may want to evaluate this option. Again modify your pom.xml to replace with this:






Here is a sample of configuration src/main/resources/logback.xml to get things started.


<appender name=”STDOUT” class=”ch.qos.logback.core.ConsoleAppender”>


<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} – %msg%n</pattern>




<logger name=”deng” level=”DEBUG”/>


<root level=”INFO”>

<appender-ref ref=”STDOUT” />





for db logging



The next step was to change logback.xml file:

<?xml version="1.0" encoding="UTF-8"?>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
    <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
            <password>root</password> <!-- no password -->

    <!-- the level of the root level is set to DEBUG by default. -->
    <root level="TRACE">
        <appender-ref ref="stdout" />
        <appender-ref ref="db" />

As seen here, I have created two appenders – a console appender and a database appender.
The database appender here requires the JDBC driver, the jdbc url and the db credentials. An additional property is the connectionSource which is actually the type of Connection wrapper that we would like to use. Logback provides a few options here and I went with the DriverManagerConnectionSource class.
The next step was to write a test class to test the code:

public class SampleTestDbAppender {

   private static final Logger logger = LoggerFactory.getLogger(TestDbAppender.class);

   public  SampleTestDbAppender () {"Class instance created at {}",  DateFormat.getInstance().format(new Date()));

   public void doTask() {
      logger.trace("In test  doTask");
      logger.trace("doTask test  complete");

   public static void main(String[] args) {
      logger.warn("Running test code...");
      new TestDbAppender().doTask();
      logger.debug("test Code execution complete.");



get script from here
This resulted in three tables:



— Logback: the reliable, generic, fast and flexible logging framework.
— Copyright (C) 1999-2010, All rights reserved.
— See for the applicable licensing
— conditions.
— This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender
— The event_id column type was recently changed from INT to DECIMAL(40)
— without testing.
DROP TABLE logging_event_property
DROP TABLE logging_event_exception
DROP TABLE logging_event
CREATE TABLE logging_event
timestmp DECIMAL(20) NOT NULL,
formatted_message VARCHAR(4000) NOT NULL,
logger_name VARCHAR(254) NOT NULL,
level_string VARCHAR(254) NOT NULL,
thread_name VARCHAR(254),
reference_flag SMALLINT,
arg0 VARCHAR(254),
arg1 VARCHAR(254),
arg2 VARCHAR(254),
arg3 VARCHAR(254),
caller_filename VARCHAR(254) NOT NULL,
caller_class VARCHAR(254) NOT NULL,
caller_method VARCHAR(254) NOT NULL,
caller_line CHAR(4) NOT NULL,
event_id DECIMAL(40) NOT NULL identity,
PRIMARY KEY(event_id)
CREATE TABLE logging_event_property
event_id DECIMAL(40) NOT NULL,
mapped_key VARCHAR(254) NOT NULL,
mapped_value VARCHAR(1024),
PRIMARY KEY(event_id, mapped_key),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
CREATE TABLE logging_event_exception
event_id DECIMAL(40) NOT NULL,
trace_line VARCHAR(254) NOT NULL,
PRIMARY KEY(event_id, i),
FOREIGN KEY (event_id) REFERENCES logging_event(event_id)



While this was the console appender, The db appender wrote entries to the table:

Please check 3 tables


I’m a SharePoint Solutions /SSIS /Biztalk/SQL/ASP.Net Developer currently working for Interpublic Group . I currently specialize in all the integration aspects of Biztalk,
implementing EAI solutions, In SharePoint dvwp, creating webparts, and creating things with jQuery. For 10+ years, I have developed software solutions that
add business value and create cost saving opportunities. I am very
experienced in BizTalk Server 2006/2009/2010/2013 and am consistently recognized by
others for exceeding expectations in the delivery of quality solutions.

MS Software Engineering: Texas University

Technical Certifications

MCTS Biztalk 2006/ 2010

Jin Thakur