Solved

get color in between two other colors

Posted on 2010-09-02
7
610 Views
Last Modified: 2012-05-10
I want to calculate the rgb value for a color IN BETWEEN two other colors. and also I want to be able to define how "close" to color2 the returned color is.
Basically I want to define the two "ends" of a gradient spectrum, and then be able to calculate the color anywhere inbetween.

How do I do this?

example:
color1 = RBG(256,256,256)
color2 = RBG(256, 0, 200)
inBetweenColor = calculateColorInBetween(color1,color2, 0.2)
so in this example I wold want the returned color to be 20% closer to color2 from color1

could you show me a function to do this with any two colors?
thanks!
0
Comment
Question by:NiklasMoller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 50
ID: 33585622
Niklas,

20% closer according to what? Hue? Saturation? Red, green or blue?

There is no definite "in between" for two different colors. You need to define what your "in between" is.

This goes beyond the scope of Excel or Office, into Color Management (with capitals).

The rules and regulations need to be provided by you, then someone here may come up with the required code.

So, what is the RGB value "between" (R256, G256, B256) and (R256, G0, B200)? What color value needs to change by how much to be the "between" value, and what are the rules?

cheers, teylyn
0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 33585672
ehmmm.... dont know really.. hehe

lets say I want to go from white to yellow.
so all colors in between would be more and more intense yellows... ?

similar to the default color palette in excel where you can choose different intense versions of red, purple, brown etc.

does that help you to understand what Im getting at?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33585811
Try this.
Just paste it as code for a sheet and see if the functions in there help.
Function ColorToRGB(ByVal color As Long) As Variant
Dim colorComponents(2) As Integer
colorComponents(0) = color Mod 256
colorComponents(1) = Fix((color Mod 65536) / 256)
colorComponents(2) = Fix(color / 65536)
ColorToRGB = colorComponents
End Function

Function Mix2(ByVal num1 As Long, ByVal num2 As Long, ByVal percent_from1 As Long)
Mix2 = num1 * (100# - percent_from1) / 100# + num2 * percent_from1 / 100#
End Function

Sub x()
color1 = RGB(0, 255, 255)
color2 = RGB(255, 0, 200)
' validate color components
rgb1 = ColorToRGB(color1)
rgb2 = ColorToRGB(color2)
Range(Cells(1, 1), Cells(3, 1)) = rgb1
Range(Cells(4, 1), Cells(6, 1)) = rgb2

' color option 2
color3 = RGB(Mix2(rgb2(0), rgb1(0), perc_closer_to_color2), _
            Mix2(rgb2(1), rgb1(1), perc_closer_to_color2), _
            Mix2(rgb2(2), rgb1(2), perc_closer_to_color2))
' show colors
Cells(1, 3).Interior.color = color1
Cells(2, 3).Interior.color = color2
Cells(3, 3).Interior.color = color3

' from color1 to color2 in 10 steps
Cells(1, 4).Interior.color = color1
For i = 1 To 10
'color3 = Cells(i + 1, 4).Interior.color
color3 = RGB(Mix2(rgb1(0), rgb2(0), i * 10), _
            Mix2(rgb1(1), rgb2(1), i * 10), _
            Mix2(rgb1(2), rgb2(2), i * 10))
Cells(i + 1, 4).Interior.color = color3
Range(Cells(i + 1, 5), Cells(i + 1, 7)) = ColorToRGB(color3)
Next

End Sub

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 45

Expert Comment

by:patrickab
ID: 33585909
NiklasMoller,

Attached is a file which might help you assess just what you mean or want by way of colours. I'm afraid the file is rather large.

Patrick
colours-interior-colours-02.xls
0
 
LVL 45

Expert Comment

by:patrickab
ID: 33585933
The attached file is the same but you will need to run the macro to see the results - it takes about 20 seconds on my machine - however the file is much smaller.

Patrick
colours-interior-colours-03.xls
0
 
LVL 5

Accepted Solution

by:
NiklasMoller earned 0 total points
ID: 33586287
I created my own function which seems to do what I want... Thanks for helping me though. I will award points for helping out, but I will accept my own code as solution! thankyou!
Function getInbetweenColor(color1 As Long, color2 As Long, movePercentTowardsColor2 As Single) As Long

    'Get colorinbetween

    Dim red1 As Long
    Dim green1 As Long
    Dim blue1 As Long
    
    Dim red2 As Long
    Dim green2 As Long
    Dim blue2 As Long
    
    Dim color1Ok As Boolean
    Dim color2Ok As Boolean
    
    If RGBComponentsFromRGBLongToVariables(color1, red1, green1, blue1) = True And _
       RGBComponentsFromRGBLongToVariables(color2, red2, green2, blue2) = True Then
        'MsgBox "getInbetweenColor() colors ok"
    Else
        'MsgBox "getInbetweenColor() ERROR One or both colors NOT OK! cannot calculate. exit function."
        Exit Function
    End If
    
    'compare values, get differences
    
    Dim redDiff As Integer
    Dim greenDiff As Integer
    Dim blueDiff As Integer
    
    redDiff = red2 - red1
    greenDiff = green2 - green1
    blueDiff = blue2 - blue1
    
    'MsgBox "Diff RGB: " & redDiff & ", " & greenDiff & ", " & blueDiff
    
    Dim newRed As Integer
    Dim newGreen As Integer
    Dim newBlue As Integer
    
    'MsgBox redDiff * movePercentTowardsColor2
    
    newRed = red1 + (redDiff * movePercentTowardsColor2)
    newGreen = green1 + (greenDiff * movePercentTowardsColor2)
    newBlue = blue1 + (blueDiff * movePercentTowardsColor2)
    
    'MsgBox "New RGB: " & newRed & ", " & newGreen & ", " & newBlue
    
    getInbetweenColor = RGB(newRed, newGreen, newBlue)

End Function

Open in new window

0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 33586320
I could not split the points. some wierd bug in EE, sorry. Thanks for helping me!
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question