C: brightyellow and brightgreen colors (ncurses)? - c

I have been looking for nice colors such as mc has for yellow and green.
In ncurses, you can specify those RGB:
10.2. Changing Color Definitions
The function init_color()can be used to change the rgb values for the
colors defined by curses initially. Say you wanted to lighten the
intensity of red color by a minuscule. Then you can use this function
as
init_color(COLOR_RED, 700, 0, 0);
/* param 1 : color name
* param 2, 3, 4 : rgb content min = 0, max = 1000 */
I couldnt find bright yellow or bright green colors. Aren't they included by default in ncurses?
Regards,
Ulrich

You can have "full" color but you need to recompile your ncurses to support it (and have something that can show it)
"To compile NCurses with 256 color support, use this option:"
--enable-ext-colors
ref: http://www.c-for-dummies.com/ncurses/256color.php according to Thomas Dickey, NCurses' maintainer and Dan Gookin
Color Function Constant Name
0 init_color(0,0,0,0); COLOR_BLACK
1 init_color(0,1000,0,0); COLOR_RED
2 init_color(0,0,1000,0); COLOR_GREEN
3 init_color(0,1000,1000,0); COLOR_YELLOW
4 init_color(0,0,0,1000); COLOR_BLUE
5 init_color(0,1000,0,1000); COLOR_MAGENTA
6 init_color(0,0,1000,1000); COLOR_CYAN
7 init_color(0,1000,1000,1000); COLOR_WHITE
It is possible to get more than the standard 8 (or 16) colors out of NCurses...
Your TERM environment variable may also be xterm try changing that to xterm-256color

The number of colors and color pairs (bg + fg) supported by the terminal is returned by functions:
COLOR()
COLOR_PAIRS()
As solusipse correctly points out, there should be 8 colors by default (there are only 8 defined in curses.h).
The init_color() call might be supported by the terminal, but most probably it won't. You can check it issuing a call to:
can_change_color().
Pubby correctly pointed out, that bolding (with A_BOLD) can brighter the text, allowing you for a total of 16 colors, though only for the foreground color.
On my system, xterm, gnome-terminal and konsole behave exactly the same; they report 8 colors, 64 color pairs, can_change_color() is false and the A_BOLD attribute indeed lightens the fg color.

main()
{
initscr();
if (start_color() == OK)
{
init_pair (1, COLOR_RED, COLOR_GREEN);
attron (COLOR_PAIR (1));
if (init_color (COLOR_RED, 0, 0, 1000) == OK)
addstr ("BLUE ON GREEN");
else
addstr ("RED ON GREEN");
getch ();
}
endwin();
}

The thing that you could do is to create a new color which would have teh name
COLOR_BRIGHT_YELLOW to the color number 8
COLOR_BRIGHT_GREEN to the color number 9
but I have never made it and it is not so much documented.

If you want bright colors, then you must use:
attrxxx(COLOR_PAIR(COLOR_xxxxx) | A_BOLD)
So to get bright yellow, one would use:
attr_t color_bright_yellow = COLOR_PAIR(COLOR_YELLOW) | A_BOLD;
etc.
(Note if you want more colors, compile [to be more specific, ./configure] ncurses with --with-ext-colors enabled.)

Related

Solr sort term based on two fields with an AND

I am running a solr query against a large group of cats. The cats have four relevant attributes:
Eye color (string)
Stray (1 yes/0 no)
Relevancy (integer 1-99)
Age(integer representing milliseconds).
I want my sort results to be the following:
Cats with green eyes AND a stray
Relevancy
Age
So my results will look like this:
Green, 1, 50, 300000
Green, 1, 25, 500000
Green, 1, 25, 100000
Blue, 1, 99, 500000
Green, 0, 98, 500000
Red, 1, 98, 400000
Green, 0, 98, 399999
I don't care the color of the eyes or stray status, unless it is both green eyes and a stray.
This sort parameter works but is missing the "stray" flag:
if(exists(query({!v=eyes:"Green"})),1,0)+desc,
relevancy+desc,
age+desc
This sort parameter doesn't work and throws an error:
if((exists(query({!v=eyes:"Green"}))) AND
(exists(query({!v=stray:1}))),1,0)+desc,
relevancy+desc,
age+desc
I'm really lost here, not sure if I am using sort incorrectly, or if I am misunderstanding syntax.
Error Message: Can't determine a Sort Order (asc or desc) in sort spec 'if((exists(query({!v=eyes:"Green"})) AND exists(query({!v=stray:1}))),1,0) desc,relevancy desc,age desc'
I figured it out for my case, it was a nest if that was needed:
if(exists(query({!v=eyes:"Green"})),if(exists(query({!v=stray:1})),1,0),0)+desc,
I might be using an out of date version of solr, that's probably why #MatsLindh answer did not work for me

How to display an array with textbox in a figure?

I'm trying to display an array as a figure in MATLAB using coloured textbox that varies according to the value at that location.
So far, I have tried to use the MATLAB Edit Plot Tool to draw such a figure and then generate the code to see what it might look like. Here is what I came up with:
figure1=figure
annotation(figure1,'textbox',...
[0.232125037302298 0.774079320113315 0.034810205908684 0.0410764872521246],...
'String','HIT',...
'FitBoxToText','off',...
'BackgroundColor',[0.470588235294118 0.670588235294118 0.188235294117647]);
annotation(figure1,'textbox',...
[0.27658937630558 0.774079320113315 0.034810205908684 0.0410764872521246],...
'String',{'STAY'},...
'FitBoxToText','off',...
'BackgroundColor',[1 0 0]);
Here the result does not look so good. I'd like something neat and not as hard to write. Visually, I'd like something like this:
I've found a possible solution using the pcolor function.
Warning: I've tested it only with Octave
If you want to create a (m x n) table with, as per your picture, 4 colour, you have to:
create an array with size (m+1 x n+1) of integers' in the1:4` range setting them according to the desired order
call pcolor to plot the table
adjust the size of the figure
create your own colormap according to the desired colors
set the `colormap'
add the desired text using the text function
set the tick and ticklabel of the axes
Edit to answer the comment
In the following you can find a possible implementation of the proposed solution.
The code creates two figure:
In the first one wil be ploted the values of the input matrix
In the second one the user defined strings
The association "color - value" is performed through the user-defined colormap.
Since in the matrix x there are 4 different possible values (it has been defined as x=randi([1 4],n_row+1,n_col+1);) the colormap has to consists of 4 RGB entry as follows.
cm=[1 0.3 0.3 % RED
0.3 0.3 1 % BLUE
0 1 0 % GREEN
1 1 1]; % WHITE
Should you want to change the association, you just have to change the order of the rows of the colormap.
The comments in the code should clarify the above steps.
Code updated
% Define a rnadom data set
n_row=24;
n_col=10;
x=randi([1 4],n_row+1,n_col+1);
for fig_idx=1:2
% Open two FIGURE
% In the first one wil be ploted the values of the input matrix
% In the second one the user defined strings
figure('position',[ 1057 210 606 686])
% Plot the matrix
s=pcolor(x);
set(s,'edgecolor','w','linewidth',3)
% Define the colormap
%cm=[1 1 1
% 0 1 0
% 0.3 0.3 1
% 1 0.3 0.3];
cm=[1 0.3 0.3 % RED
0.3 0.3 1 % BLUE
0 1 0 % GREEN
1 1 1]; % WHITE
% Set the colormap
colormap(cm);
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 1);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'SUR');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 2);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'DBL');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 3);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'HIT');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 4);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'STK');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Create and set the X labels
xt=.5:10.5;
xtl={' ';'2';'3';'4';'5';'6';'7';'8';'9';'10';'A'};
set(gca,'xtick',xt);
set(gca,'xticklabel',xtl,'xaxislocation','top','fontweight','bold');
% Create and set the X labels
yt=.5:24.5;
ytl={' ';'Soft20';'Soft19';'Soft18';'Soft17';'Soft16';'Soft15';'Soft14';'Soft13'; ...
'20';'19';'18';'17';'16';'15';'14';'13';'12';'11';'10';'9';'8';'7';'6';'5'};
set(gca,'ytick',yt);
set(gca,'yticklabel',ytl,'fontweight','bold');
title('Dealer''s Card')
end
Table with the values in the input matrix
Table with the user-defined strings
This is an answer inspired by il_raffa's answer, but with also quite a few differences. There is no better or worse, it's just a matter of preferences.
Main differences are:
it uses imagesc instead of pcolor
it uses a second overlaid axes for fine control of the grid color/thickness/transparency etc...
The association between value - label - color is set right at the beginning in one single table. All the code will then respect this
table.
It goes like this:
%% Random data
n_row = 24;
n_col = 10;
vals = randi([1 4], n_row, n_col);
%% Define labels and associated colors
% this is your different labels and the color associated. There will be
% associated to the values 1,2,3, etc ... in the order they appear in this
% table:
Categories = {
'SUR' , [1 0 0] % red <= Label and color associated to value 1
'DBL' , [0 0 1] % blue <= Label and color associated to value 2
'HIT' , [0 1 0] % green <= Label and color associated to value 3
'STK' , [1 1 1] % white <= you know what this is by now ;-)
} ;
% a few more settings
BgColor = 'w' ; % Background color for various elements
strTitle = 'Dealer''s Card' ;
%% Parse settings
% get labels according to the "Categories" defined above
labels = Categories(:,1) ;
% build the colormap according to the "Categories" defined above
cmap = cell2mat( Categories(:,2) ) ;
%% Display
hfig = figure('Name',strTitle,'Color',BgColor,...
'Toolbar','none','Menubar','none','NumberTitle','off') ;
ax1 = axes ;
imagesc(vals) % Display each cell with an associated color
colormap(cmap); % Set the colormap
grid(ax1,'off') % Make sure there is no grid
% Build and place the texts objects
textStrings = labels(vals) ;
[xl,yl] = meshgrid(1:n_col,1:n_row);
hStrings = text( xl(:), yl(:), textStrings(:), 'HorizontalAlignment','center');
%% Modify text color if needed
% (White text for the darker box colors)
textColors = repmat(vals(:) <= 2 ,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
%% Set the axis labels
xlabels = [ cellstr(num2str((2:10).')) ; {'A'} ] ;
ylabels = [ cellstr(num2str((5:20).')) ; cellstr(reshape(sprintf('soft %2d',[13:20]),7,[]).') ] ;
set(ax1,'XTick', 1:numel(xlabels), ...
'XTickLabel', xlabels, ...
'YTick', 1:numel(ylabels), ...
'YTickLabel', ylabels, ...
'TickLength', [0 0], ...
'fontweight', 'bold' ,...
'xaxislocation','top') ;
title(strTitle)
%% Prettify
ax2 = axes ; % create new axe and retrieve handle
% superpose the new axe on top, at the same position
set(ax2,'Position', get(ax1,'Position') );
% make it transparent (no color)
set(ax2,'Color','none')
% set the X and Y grid ticks and properties
set(ax2,'XLim',ax1.XLim , 'XTick',[0 ax1.XTick+0.5],'XTickLabel','' ,...
'YLim',ax1.YLim , 'YTick',[0 ax1.YTick+0.5],'YTickLabel','' ,...
'GridColor',BgColor,'GridAlpha',1,'Linewidth',2,...
'XColor',BgColor,'YColor',BgColor) ;
% Make sure the overlaid axes follow the underlying one
resizeAxe2 = #(s,e) set(ax2,'Position', get(ax1,'Position') );
hfig.SizeChangedFcn = resizeAxe2 ;
It produces the following figure:
Of course, you can replace the colors with your favorite colors.
I would encourage you to play with the grid settings of the ax2 for different effects, and you can also play with the properties of the text objects (make them bold, other color etc ...). Have fun !

Threads synchronization in C

I'm trying to make a project as follows:
I'll have 10 threads, each controlling a column of 10 rectangular box.(10 columns of 10 boxes each) And at every 3s or so, I need 2 of them to change colour(i.e. red), such that at any point of time, there should be 2 red columns of 10 red boxes out of 10.
The current code I have for the column of boxes:
void* thread_func_1() {
while (1) {
if (randomNo1 == 1 || randomNo2 == 1) {
sema_wait(&sem) ;
drawRedBoxes();
sleep(3000);
drawBlueBoxes();
sema_post(&sem);
randomize(); // random 2 more numbers
}else{
drawBlueBoxes();
}
}
void randomize(){
do{
randomNo1 = 1 + rand()/ (RAND_MAX/(10) + 1);
randomNo2 = 1 + rand()/ (RAND_MAX/(10) + 1);
} while (randomNo1 == randomNo2);
}
Edit: All the 10 threads are of similar code. The drawBoxes function draws the 10 rectangular boxes in the column.
Not the exact code but it is something like this. Ideally I would want the 2 new red columns to appear together(or at least a very very short time delay between them), but I realised after awhile, it is really obvious that 1 red colum appear some time after another. How can I solve this problem?

How can i color my text in orange color?

I'm trying to find how to color my text in orange.
I tried this:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_GREEN);
But it will give me a red on green text.
Is it even possible to get the color orange? i tried different codes i found in the internet, but none of them gave me orange.
thanks for the help.
You can try the following bit mask: FOREGROUND_RED | FOREGROUND_GREEN.
It will change the color of the text to orange. You may still want to experiment with BACKGROUND_* to adjust the background color to your needs.
` there is another way to change the color of text,,,,
infact you can also change the color of console.
use system("color xy") where x is the background color and y is the text color and dont forget to include dos and windows header file
here is the color table
FOR EXample
#include <windows.h> /* code table 0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White */
#include<dos.h>
system("color 12");`

Batch File color mixing (part 2)

I tried this code to mix colors and may someone please describe the code to me?
#echo off
setlocal EnableDelayedExpansion
set hexa=0123456789ABCDEF
set /P "first=Enter first color (hexa digit): "
set /P "second=Enter second color (hexa digit): "
set /A sum= (0x%first% + 0x%second%) %% 16
set result=!hexa:~%sum%,1!
color %result%
echo The result is: %result%
I know this is part of my 1st question, but I just need help on how to use it correctly.
I'm sorry for asking this stupid question in the first place... I was doing a ton of research and some of them didnt work out properly and i was hoping for easier ways to get what I wanted. Sorry guys =(
In the original question the user ask for "mix 2 batch colors into another color (for example: Red + Yellow to make Orange)". I answered with the color table of color command, that uses 16 different colors with values from 0 to F in hexadecimal (equivalent to 0 to 15 in decimal):
0 = Black 8 = Gray
1 = Blue 9 = Light blue
2 = Green A = Light green
3 = Aqua B = Light aqua
4 = Red C = Light red
5 = Magenta D = Light magenta
6 = Brown E = Yellow
7 = White F = Bright white
I elaborated on his example: Red value is 4 + Yellow value is E (decimal 14) = 12 in hexadecimal (18 decimal). This result is outside of the valid color range so an adjustment is needed, and the usual way to do this adjust is taking the remainder of the large number when it is divided by the base value, 16 in this case. This is what this line do:
set /A sum= (0x%first% + 0x%second%) %% 16
You may enter set /? for further description of previous line. This way, the remainder when 18 decimal is divided by 16 decimal is 2, that correspond to Green color. In hexadecimal notation is easier to get the remainder because it is just the last digit. Another way to get this remainder is starting at the first value in previous table and jump the number of colors of second value, returning to first color (0) when the table ends.
You may try other "color mixing" cases, for example: Blue (1) + Brown (6) = White (7); Aqua (3) + Light blue (9) = Light red (C, decimal 12); Magenta (5) + Light magenta (D, decimal 13) = 12 (decimal 18) = Green (2).
Note that if you "add" Grey color (8) to any other color, the result is a switching between the dark and light versions of that color.
Ohk, heres a step by step of your code:
#echo off
Turns Echo off meaning any commands executed can't be seen by the user, instead only input and output can be seen
setlocal EnableDelayedExpansion
Allows the use of ! to Expand Variables if % has already been Expanded
set hexa=0123456789ABCDEF
Creates a variable hexa with value of all valid hexadecimal digits.
set /P "first=Enter first color (hexa digit): "
Takes one line of input prompting with the above text, and sets first to it.
set /P "second=Enter second color (hexa digit): "
Takes one line of input prompting with the above text, and sets seconds to it.
set /A sum= (0x%first% + 0x%second%) %% 16
creates a variable sum which is set to the solution to the above equation
set result=!hexa:~%sum%,1!
Creates a variable result which is set to hexa, from the sumth index and one letter ahead.
color %result%
sets the screen colour to the hexadecimal value of result
echo The result is: %result%
Outputs to the console the above text including the value of the variable result.
That explains it quite well, and if you want this code to do something else, feel free the ask.
Mona.
Color of CMD window can be customized like following (I take some sentences from 'color' command help):
Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
So the color can be specified by following command:
color F9
where F is the background color and 9 is the text color.
The rest in the code you posted is taking values from input and call this command.

Resources