Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

translate color name to RGB?

50 views
Skip to first unread message

Stephen Vavasis

unread,
Feb 7, 1999, 3:00:00 AM2/7/99
to
I would like to write a Tcl-callable function (either in C or Tcl) named
colortranslate to translate a named color into r,g,b. It would work
something like this:

% colortranslate darkmagenta
139 0 139

The good news is that there is a function Tk_GetColor that already
performs this task, so all I have to do to is wrap it with a command:
int colortranslate(ClientData, Tcl_Interp* interp, int, Tcl_Obj*) {
/* ... */
XColor* co = Tk_GetColor(interp, tkwin, name);
/* ... */
return TCL_OK;
}

The bad news is that I don't know how to find a tkwin argument necessary
for invoking Tk_GetColor. Any suggestions?

I am willing to pass the string name of an existing window to my
function (so that my function call would now be: "colortranslate .c
darkmagenta"), but I don't know how to convert the name of a window to a
Tk_Window object. There is another function, Tk_NameToWindow for the
latter purpose, but this function also needs a tkwin argument! Kind of
a chicken-and-egg problem. (Note: I am quite familiar with the C/Tcl API
but not with the C/Tk API.)

Thanks,
Steve Vavasis

Christopher Nelson

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to
Stephen Vavasis wrote:
>
> I would like to write a Tcl-callable function (either in C or Tcl) named
> colortranslate to translate a named color into r,g,b. It would work
> something like this:
>
> % colortranslate darkmagenta
> 139 0 139
>
> ...

Is there something wrong with [winfo rgb . darkmagenta]?

Chris
--
Rens-se-LEER is a county. RENS-se-ler is a city. R-P-I is a school!

Bryan Oakley

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to
Christopher Nelson wrote:
>
> Stephen Vavasis wrote:
> >
> > I would like to write a Tcl-callable function (either in C or Tcl) named
> > colortranslate to translate a named color into r,g,b. It would work
> > something like this:
> >
> > % colortranslate darkmagenta
> > 139 0 139
> >
> > ...
>
> Is there something wrong with [winfo rgb . darkmagenta]?

He seems to want a tcl (ie: non-tk) solution.

--
Bryan Oakley mailto:oak...@channelpoint.com
ChannelPoint, Inc. http://purl.oclc.org/net/oakley

Gerald...@cpu.com

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to vav...@cs.cornell.edu
In article <36BE3E...@cs.cornell.edu>,

vav...@cs.cornell.edu wrote:
> I would like to write a Tcl-callable function (either in C or Tcl) named
> colortranslate to translate a named color into r,g,b. It would work
> something like this:
>
> % colortranslate darkmagenta
> 139 0 139
>...
> The bad news is that I don't know how to find a tkwin argument necessary
> for invoking Tk_GetColor. Any suggestions?
>
> I am willing to pass the string name of an existing window to my
> function (so that my function call would now be: "colortranslate .c
> darkmagenta"), but I don't know how to convert the name of a window to a
> Tk_Window object. There is another function, Tk_NameToWindow for the
> latter purpose, but this function also needs a tkwin argument! Kind of
> a chicken-and-egg problem. (Note: I am quite familiar with the C/Tcl API
> but not with the C/Tk API.)

If all you really want to do is translate the colorname to an rgb value, as
specified. You can do this all in Tcl/Tk. Here is the code:

proc colortranslate {color} {
return [winfo rgb . $color]
}

If it really was how to get a tkwin item, please repost with that request.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Bryan Oakley

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
Gerald...@cpu.com wrote:
> If all you really want to do is translate the colorname to an rgb value, as
> specified. You can do this all in Tcl/Tk. Here is the code:
>
> proc colortranslate {color} {
> return [winfo rgb . $color]
> }
>
> If it really was how to get a tkwin item, please repost with that request.

... assuming you have tk loaded. This won't work in tclsh. It was my
impression that the original poster wanted a pure tcl (ie: non-tk)
solution. I could be wrong.

Donal K. Fellows

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
In article <36C07787...@channelpoint.com>,

Bryan Oakley <oak...@channelpoint.com> wrote:
> Gerald...@cpu.com wrote:
>> If all you really want to do is translate the colorname to an rgb
>> value, as specified. You can do this all in Tcl/Tk. Here is the
>> code:
>> proc colortranslate {color} {
>> return [winfo rgb . $color]
>> }
>> If it really was how to get a tkwin item, please repost with that request.
>
> ... assuming you have tk loaded. This won't work in tclsh. It was my
> impression that the original poster wanted a pure tcl (ie: non-tk)
> solution. I could be wrong.

Well, the original poster was muttering about a tkwin argument to
Tk_GetColor, so I would presume that he's already got Tk loaded. The
routine he's after is, of course, Tk_MainWindow(Tcl_Interp *interp)

Thus:
XColor color = Tk_GetColor(interp, Tk_MainWindow(interp), "skybluepink");
if (color == NULL) { /* Oops! */
return TCL_ERROR;
}
/* Use the color here */
Tk_FreeColor(color); /* Done with it now! */

Note that if you have a multi-display interpreter, this only ever uses
the main display (a problem since colours are not constant across
displays by any means...)

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
Department of Computer Science, University of Manchester, U.K. +44-161-275-6137
--
"And remember, evidence is nothing." - Stacy Strock <sp...@adisfwb.com>

Victor Wagner

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
Bryan Oakley <oak...@channelpoint.com> wrote:

: .... assuming you have tk loaded. This won't work in tclsh. It was my


: impression that the original poster wanted a pure tcl (ie: non-tk)
: solution. I could be wrong.

Really I have done some research on that subject developing GIS
extension to Tcl, but found that there is no portable solution.
You can try to find rgb.txt file in some predefined places, but
note that your program might run on server where no X installed.


: --


: Bryan Oakley mailto:oak...@channelpoint.com
: ChannelPoint, Inc. http://purl.oclc.org/net/oakley

--
--------------------------------------------------------
I have tin news and pine mail...
Victor Wagner @ home = vi...@wagner.rinet.ru

0 new messages