Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Visualize a Weighted Average / Composite Color of Multiple Custom Colors Using C#

0.00/5 (No votes)
29 Apr 2015 2  
Combine weighted values of four custom colors to get their visual average

Colors Just Wanna Have Fu-un

There is a 'seminar' of sorts (not sure what it really should be called) where you learn about different personality types (your own and others) which is supposed to help you understand yourself and others better, and thus be able to work more effectively together. It is called "True Colors".

I wondered, after attending this class/session, what my "composite" color would be. I found out that "my" "primary" color is green, and my "palest" color is orange, but what about gradations in between (which we all are, made up of "a little of this color and a little of that color")?

I wanted to visualize "my" color. So I created a simple util that purports to do that.

Note: A way to accomplish this using web technologies can be seen in the accepted answer (by Michael Laszlo) here.

Let it be said first, that the colors used here (red, blue, orange, and gold) are not necessarily what a professional artist, printer, interior decorator, and so forth, would call those colors. They are the "True Colors" hues as extracted from their website using the ColorZilla browser plugin.

But, using this code, you could use the principles and practices herein to use your own custom colors (or the "official" versions of red, blue, orange, gold, etc.) to come up with different representations of coordinates on the electromagnetic spectrum.

This is only a tip, so I'm just adding the code from this point; there are a handful of comments in it, though - let them serve as the tip explanation; As Mark Twain would have said: "Let it go." And so here it is, without further ado or adon't, the code:

C#
// Note that the results differ from "true" blue, green, orange, and gold; 
//   the colors used to represent these are custom 

versions of those colors, as found on the True Colors website
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CyndiLauper
{
    public partial class FormTrueColorsMain : Form
    {
        // Primary constituents of the "True Colors" hues
        const int GREEN_RED = 15;
        const int GREEN_GREEN = 167;
        const int GREEN_BLUE = 73;
        const int BLUE_RED = 0;
        const int BLUE_GREEN = 152;
        const int BLUE_BLUE = 215;
        const int ORANGE_RED = 243;
        const int ORANGE_GREEN = 123;
        const int ORANGE_BLUE = 38;
        const int GOLD_RED = 255;
        const int GOLD_GREEN = 230;
        const int GOLD_BLUE = 101;

        Color tcGreen = Color.FromArgb(15, 167, 73);
        Color tcGreenComplement = Color.FromArgb(240, 88, 182);
        Color tcBlue = Color.FromArgb(0, 152, 215);
        Color tcOrange = Color.FromArgb(243, 123, 38);
        Color tcGold = Color.FromArgb(255, 230, 101);
        
        public FormTrueColorsMain()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            lblGreen.ForeColor = tcGreen;
            lblGreen.BackColor = Color.White; // tcGreenComplement;
            lblBlue.ForeColor = tcBlue;
            lblBlue.BackColor = Color.White;
            lblOrange.ForeColor = tcOrange;
            lblOrange.BackColor = Color.Black;
            lblGold.ForeColor = tcGold;
            lblGold.BackColor = Color.Black;
            // For these to work, needed to also comment out 
            // "Application.EnableVisualStyles();" in Program.cs 
            // (see Crispy's answer at 
            // http://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-sharp-net-3-5)
            progressBarGreen.ForeColor = tcGreen;
            progressBarGreen.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            progressBarBlue.ForeColor = tcBlue;
            progressBarBlue.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            progressBarOrange.ForeColor = tcOrange;
            progressBarOrange.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            progressBarGold.ForeColor = tcGold;
            progressBarGold.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // If they have already been set 
            // (user clicked the arrows rather than entered the value directly), this is moot but it's
            // probably not worthwhile checking to see whether their value is 0, 
            // and only calling the methods conditionally in that case.
            SetGreenProgressBar();
            SetBlueProgressBar();
            SetOrangeProgressBar();
            SetGoldProgressBar();

            int greenVal = (int)numericUpDownGreen.Value;
            int blueVal = (int)numericUpDownBlue.Value;
            int orangeVal = (int)numericUpDownOrange.Value;
            int goldVal = (int)numericUpDownGold.Value;

            Color trueColor = GetTrueCombinedColor(greenVal, blueVal, orangeVal, goldVal);
            pictureBoxTCCombo.BackColor = trueColor;
        }

        private Color GetTrueCombinedColor(int greenVal, int blueVal, int orangeVal, int goldVal)
        {
            // tcBlue has no red, so can disregard blueVal for redTotal
            int redTotal = ((greenVal * GREEN_RED) + 
            (orangeVal * ORANGE_RED) + (goldVal * GOLD_RED));
            int greenTotal = ((greenVal * GREEN_GREEN) + 
            (blueVal * BLUE_GREEN) + (orangeVal * ORANGE_GREEN) + (goldVal * GOLD_GREEN));
            int blueTotal = ((greenVal * GREEN_BLUE) + 
            (blueVal * BLUE_BLUE) + (orangeVal * ORANGE_BLUE) + (goldVal * GOLD_BLUE));

            int redWeight = redTotal / 50;
            int greenWeight = greenTotal / 50;
            int blueWeight = blueTotal / 50;
            if (redWeight <= 0) redWeight = 1;
            if (greenWeight <= 0) greenWeight = 1;
            if (blueWeight <= 0) blueWeight = 1;
            if (redWeight >= 256) redWeight = 255;
            if (greenWeight >= 256) greenWeight = 255;
            if (blueWeight >= 256) blueWeight = 255;

            Color trueColorCombo = Color.FromArgb(redWeight, greenWeight, blueWeight);
            //Color trueColorCombo = Color.FromArgb(redWeight, greenWeight, blueWeight, 10);
            return trueColorCombo;
        }

        private void numericUpDownGreen_ValueChanged(object sender, EventArgs e)
        {
            SetGreenProgressBar();
        }

        private void SetGreenProgressBar()
        {
            progressBarGreen.Value = (int)numericUpDownGreen.Value;
        }

        private void numericUpDownBlue_ValueChanged(object sender, EventArgs e)
        {
            SetBlueProgressBar();
        }

        private void SetBlueProgressBar()
        {
            progressBarBlue.Value = (int)numericUpDownBlue.Value;
        }

        private void numericUpDownOrange_ValueChanged(object sender, EventArgs e)
        {
            SetOrangeProgressBar();
        }

        private void SetOrangeProgressBar()
        {
            progressBarOrange.Value = (int)numericUpDownOrange.Value;
        }

        private void numericUpDownGold_ValueChanged(object sender, EventArgs e)
        {
            SetGoldProgressBar();
        }

        private void SetGoldProgressBar()
        {
            progressBarGold.Value = (int)numericUpDownGold.Value;
        }
    }
}

And here's what it looks like, in action (inaction?):

True Colors Shining Through

Time After Time

Let your True Colors shine through by sending me a Persian Rug made up of the True Colors of yourself, your family, your workmates, and your local police dog.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here