Hello,
For a project I am working on, I wrote a piece of code in VBA backend of Excel to convert RGB colour values to HSV...
The VBA is working fine, and as you can see at the bottom I am stuffing the three results generated into an array expressed (sic) as a variant...
The first value in the returned array, Hue, is dropped into the cell where the function is called (E2 in the attached screen shot..) - i.e., " =fntHSVtoRGB(E2 ,F2,G2)" In Excel do I get access to the 2nd and 3rd values in the array (i.e., saturation and value) and put in the correct adjacent cells? (i.e., cells F2 and G2, respectively, in the screen shot attached)
As you can see towards the bottom of the code, I have tested using a message box and so know that the array has them in there! Frustrating as you can imagine...
I am thinking there is some CTRL+SHIFT+ENTE R approach – but I don’t use Excel for arrays much, that is what databases are for! ;-)
I am guessing I could call three variations of the function, one for hue, saturation and value - but that does not seem very elegant!
[aside: Python has RGB --> HSV and back as a built in functions, guess it could be a good time to start using Python... <wink>]
Thanks in advance for any input!
Cheers:
GREG COCKS
---------------------------------------- -
-
Function fntRGBtoHSV(intRed As Integer, intGreen As Integer, intBlue As Integer) As Variant
-
-
'algorithm derived from http://www.easyrgb.com/index.php?X=MATH#text2
-
-
Dim aryHSLResults As Variant
-
-
Dim intMax As Integer
-
Dim intMin As Integer
-
-
Dim dblWorkingR As Double
-
Dim dblWorkingG As Double
-
Dim dblWorkingB As Double
-
-
Dim dblHue As Double
-
Dim dblSaturation As Double
-
Dim dblValue As Double
-
-
Dim dblDelta As Integer
-
-
intMax = fntMaximum(intRed, intGreen, intBlue)
-
-
intMin = fntMinimum(intRed, intGreen, intBlue)
-
-
dblDelta = intMax - intMin
-
-
dblValue = intMax
-
-
If (intMax = 0) Then
-
-
'i.e., this is a gray, no chroma
-
dblHue = 0
-
dblSaturation = 0
-
dblValue = 0
-
-
Else
-
-
'i.e., a chromatic value
-
dblSaturation = dblDelta / intMax
-
dblWorkingR = (((intMax - intRed) / 6) + (dblDelta / 2)) / dblDelta
-
dblWorkingG = (((intMax - intGreen) / 6) + (dblDelta / 2)) / dblDelta
-
dblWorkingB = (((intMax - intBlue) / 6) + (dblDelta / 2)) / dblDelta
-
-
If (intRed = intMax) Then
-
'red is max
-
dblHue = dblWorkingB - dblWorkingG
-
Else
-
If (intGreen = intMax) Then
-
'green is max
-
dblHue = (1 / 3) + dblWorkingR - dblWorkingB
-
Else
-
If (intBlue = intMax) Then
-
'blue is max
-
dblHue = (2 / 3) + dblWorkingG - dblWorkingR
-
End If
-
End If
-
End If
-
-
End If
-
-
If (dblHue < 0) Then
-
dblHue = dblHue + 1
-
Else
-
If (dblHue > 1) Then
-
dblHue = dblHue - 1
-
End If
-
End If
-
-
'convert hue to degrees
-
dblHue = dblHue * 360
-
-
'convert (sic) saturation to a percentage
-
dblSaturation = dblSaturation * 100
-
-
'convert 'value' with a distinct multipler
-
dblValue = dblValue / cstValueMultipler
-
-
'stuff the three results into an array
-
aryHSLResults = Array(dblHue, dblSaturation, dblValue)
-
-
'DRAFT @ 12/15/10 - delete this message box call
-
MsgBox ("HSV: " & aryHSLResults(0) & ", " & aryHSLResults(1) & ", " & aryHSLResults(2))
-
-
'send the values back
-
fntRGBtoHSV = aryHSLResults
-
-
End Function
-
-
0 2121 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ali Tahbaz |
last post by:
I'm having trouble iterating through LinkSources in an Excel workbook
using C#. I first wrote the below code in VBA to get a quick, correct
result,
Dim x As Variant
For Each x In ThisWorkbook.LinkSources
Debug.Print x
Next
but am so far unable to successfully convert the code to workable
|
by: MattB |
last post by:
OK, never mind my last post. It was easy enough to refer to the table the
repeater is bound to, but I made a big, incorrect assumption in that post.
In my last post I thought I was successfully getting values from the
textboxes in my repeater. It turns out I am not and I can't see what I'm
doing wrong. I've had this problem before and just ended up taking a
different route to avoid it. Now I REALLY need to figure out what I'm doing...
|
by: Kiran Kumar Pinjala |
last post by:
Hi,
May be this is silly, or I just need a second pair eyes to look at this.
I am trying to get values that I have edited in a datagrid and update the
values with those values. Here is the code that I am using in my update
command.
TextBox txtProdName = (TextBox)e.Item.Cells.Controls;
TextBox txtSuppID = (TextBox)e.Item.Cells.Controls;
TextBox txtCatID = (TextBox)e.Item.Cells.Controls;
|
by: Michael Glass |
last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework,
and I have a serious problem with the page I'm currently working on. The page
has, among other things, two FormViews and a GridView control, each with its
own SqlDataSource. FormView1 talks to my Opportunity table and has an
ItemTemplate and an EditItemTemplate. FormView2 talks to my Activities table
and has an ItemTemplate, InsertItemTemplate and an...
|
by: sathya.krishnamurthy |
last post by:
Hello Everybody
I have an C++ COM DLL with the following implementation.
__interface ICPM : IDispatch
{
HRESULT GlobalComputeClass ( VARIANT *inArr, double
*pval);
}
| |
by: Paul M. Cook |
last post by:
Let's say you have a CSV file and you load it into a variant array using the
split function on VBCrLF. Then you load a variable with the line count and
loop through the array for 0 to line count. This works well unless you have
blank lines at the end of the CSV file. Now if you process the loop, you'll
get an out of bounds subscript at the end of the loop because you are
referencing null values at the end of the variant array.
How...
|
by: dwinson |
last post by:
I am writing an add-in in C# for a server written in VB6.
In order for my add-in to work I need to implement this method:
MyMethod(string inputData)
.... so my C# code looks like this:
public object MyMethod(string inputData)...
I wrote a test harness in VB6 to test the method. I can call the method
|
by: buzzluck68 |
last post by:
Hello,
I am having trouble upgrading my VB apps to .net. When i started at
my company there was a dll created here in vb6, and i need to use that
dll for many functions that would take too much time to re-create for
the limit that i am given. I have to create a new app in .net, per my
instructions, but the dll uses variant arrays, and i keep getting an
error whenever i pass an object array from my .net app to the vb6 dll
which needs...
|
by: gellis72 |
last post by:
I'm working on a program that imports a bunch of data from a folder
full of Excel files and compiles it into an Access DB. The Excel files
have a varying number of rows and columns that need to be imported,
and the data needs to be processed/error checked before it's written
to the DB. The way I've tackled this is to have a procedure that steps
through a list of Excel files, invokes an instance of Excel, opens
file "x", copies the relevant...
|
by: Bonzs |
last post by:
I have troule with this macro... geting the used rsnge...
Public strName As String, ws As Worksheet
Sub Test()
Workbooks.Open Filename:= _
"C:\Documents and Settings\User\Desktop\IT Development\Pricer.xls"
'Begins formatting the pricer for generating the EPB...
strName = ActiveSheet.Name
'MsgBox strName
MsgBox "The Used Range of this Worksheet is: " & GetUsedRange(ws)
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |