5

I don't even know how to describe what I want, but here goes. Assume I have 3 textboxes, and I enter some colour hex code in the first one, I want to apply a black layer on top of it, and have the opacity set to 50% and get the resulting colour in the second text box. Same thing, but with white on the third one.

Let me explain: consider this image below:

enter image description here

In Photoshop, I have the base layer which is sort of sky blue in colour. I create two layers on top of it, one with black, one with white but both have an opacity of 50%. Now, I can use the colour picker (I) to simply select the two wanted colours.

I am having to do this an insane amount of times so I was wondering if I could produce it programatically.

I know, Ideally I should have tried out something, then give out the code which isn't working.. but this has stumped me enough that I don't even know where to start. The closest thing I've seen is Kuler so I think it is possible in flash at least, but then again, I don't know where to start.

Can you guys please point me in the right direction? Ideally, this would be so much better if it's doable in jQuery, but I have looked around and couldn't find anything quite like it. I am not asking for a working solution, just a nudge in the right direction.

If you have any questions, please ask.

Technology is not important to me, solution is - I am most comfortable with c# (at least I like to think I am) but I am a beginner in php, flash. Jquery, I am good at it with most stuff (well, who isn't?) - Whatever works is good to me. Php and Flash, I learnt it as a hobby just to familiarize myself.

Many thanks.

iamserious
  • 5,385
  • 12
  • 41
  • 60
  • 2
    Looking at this now in C#. Im pretty sure its doable. Just 1 point, you have an error in your image above, the black and white overlay result is the same hex code. – Jamiec Aug 31 '11 at 10:18
  • @Jamiec - oh, my mistake, will rectify it, thanks for spotting! - and it's fixed now.. – iamserious Aug 31 '11 at 10:19

3 Answers3

3

So I can get close, but not exactly your results, which I think is a side effect of .NET using a number in the range 1..255 for alpha when creating a color.

But nonetheless, I think this pretty much does what you want:

public class ColorUtility
{
    private Color color;

    public ColorUtility(Color original)
    {
        this.color = original;
    }

    public Color GetTransformedColor(Color overlay)
    {
        using(var bitmap = new Bitmap(1,1))
        {
            var g = Graphics.FromImage(bitmap);
            using(Brush baseBrush = new SolidBrush(this.color))
            {
                g.FillRectangle(baseBrush,0,0,1,1);
            }

            using(Brush overlayBrush = new SolidBrush(Color.FromArgb(127,overlay)))
            {
                g.FillRectangle(overlayBrush,0,0,1,1);
            }
            return bitmap.GetPixel(0, 0);
        }
    }
}

usage:

 var startColor = ColorTranslator.FromHtml("#359eff");
 var util = new ColorUtility(startColor);
 var blackOverlay = util.GetTransformedColor(Color.Black); // yields #9aceff
 var whiteOverlay = util.GetTransformedColor(Color.White); // yields #1b4f80

Close to your desired results, but not exactly.

EDIT: If you change the alpha value to 128 in the utility you get

Black: #9acfff
White: #1a4f7f

This might be closer to what you want, but its still not exact.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 1
    Hi, this is very close enough to what I want, so this will do! thanks! I would give you more points if I could, thanks very much again! – iamserious Aug 31 '11 at 11:37
2

I know I am late for the party, just wanted to show another way of doing it.

There is a jquery color plugin for this, I have never really used this, but there is a function that looks like it does what you want.. xColor is the plugin you are looking for.. if you go the combination section you will see that it says that it does what you want.

I just tried a sample on jsfiddle but the results are in line with Jamie's amazing answer. this gives out the same result colors as that of Jamie's code. so you can use either I guess.

Community
  • 1
  • 1
LocustHorde
  • 6,361
  • 16
  • 65
  • 94
  • this is also good answer. I have already used jamiec's code. Will keep this in mind though, thanks. – iamserious Aug 31 '11 at 13:34
  • 1
    Great answer. Well found on the jQuery plugin. Also, im starting to think our results are right and the way photoshop's color picker works is broken. – Jamiec Aug 31 '11 at 16:28
  • @Jamiec, I am not so sure, I would have thought that photoshop being a master in what it does, it would be pin point accurate on picking up a color.. but then again, c# AND jquery giving out the same result would suggest that there is something that I clearly do not understand about colors! – LocustHorde Sep 01 '11 at 09:14
  • 2
    @Locust - Perhaps its just the algorythm it uses to merge colours is slightly different to the way c# and that jQuery plugin do it. Either way, both are close enough. – Jamiec Sep 01 '11 at 09:25
0

So... What's the problem to just write what you exactly said using technology you know best? 3 boxes with colors, input for opacity percentage and output as resulting mixed colors. (I can write it on flash, but I'm not sure if prividing the whole program is appropriate on this site.)

If you don't know how to mix colors with opacity, this link should help:

http://www.pegtop.net/delphi/articles/blendmodes/opacity.htm

moropus
  • 3,672
  • 1
  • 22
  • 30
  • Hi, sorry, what language is this? Can you give some links of colour manipulation in flash? thanks. – iamserious Aug 31 '11 at 10:52
  • This is pseudocode. AFAIK, there's no tools to manipulate colors in flash on low level - only in terms of the whole bitmap. – moropus Aug 31 '11 at 10:57
  • @moropus: color manipulation is a basic thing, action script has a very strong support for it too. As if this is not enough, there are numerous plugins like [Easel](http://www.oinutter.co.uk/2011/02/13/easel-an-as3-colour-manipulation-library/), ([demo](http://www.oinutter.co.uk/easel/)) libraries that help you with it. – LocustHorde Aug 31 '11 at 13:24
  • @LocustHorde: please provide some info on how one can merge two colors (uints), say, with lighten blendmode without additional plugins and libraries. Because I don't know how to do this on pure colors. – moropus Aug 31 '11 at 13:38
  • @moropus, well, here's a simple [fading color](http://www.pixelwit.com/blog/2007/05/hexadecimal-color-fading/) tutorial - all done with action script only. Be sure to check the [basic color fading tutorial](http://www.pixelwit.com/temp/colorfadetutorial/home.html) from that page too. The idea is, if you want to add two rgb colors, you have to add the channels r, g and b seperately, to avoid spill overs of channels. Have a look at this thread: http://www.actionscript.org/forums/showthread.php3?t=242179 - esp last answer. Hope that helps. – LocustHorde Aug 31 '11 at 14:27
  • @LocustHorde, thank you, but it is not what I desire. How this functions which I should write myself (or copypaste) differs from plugin? I want built-in support and built-in tools :) So I didn't change my opinion - in flash there's no tool to manipulate colors, one should write it himself. – moropus Aug 31 '11 at 14:36
  • Oh, I see what you mean.... and it's fair enough. If you can edit your answer somehow, I can take out the negative vote. I should have thought it through. – LocustHorde Aug 31 '11 at 15:24