Question

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 =(

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top