How to plot HSV values using C++ - language-agnostic

I am pretty new to these things, I have implemented a code in C++ to find HSV values from RGB values.
R = 255;
G = 0;
B = 0;
for this i am getting the value of HSV as :
H = 0;
S = 1;
V = 1;
No i am taking here V= 255V and S = 255S whereas H = H/2. I am getting correct hsv values but i dont know how to plot them in 24 bit bitmap image. I have searched internet but could bot find. Anybody can help me out here ? Thankx

Most image formats (including bitmap) store values in RGB format (or BGR format, depending on endianness). In the case of a 24-bit bitmap, you use 8 bits for red, 8 bits for green, and 8 bits for blue, so if you want to store an image in this format, you'll have to work in RGB (or convert to RGB before saving) for the image to be colored correctly.
Now, if you don't care how the image appears, but just want to store the H, S, and V components in the image's R, G, and B channels, you can easily do so (just scale the chanels up to the desired range). So for a 24-bit bitmap, 8 bits per channel means the channels have a value between 0 and 255. Assuming H, S, and V are in the range 0 to 1, you would multiply each by 255 to put them in that range.
Below are the H, S, and V values of the Stack Overflow logo stored in an images's R, G, and B channels, respectively.

Related

How to find area enclosed by points in octave using Quadrature or any other method

I have two sets of coordinates (both positive and negative values, not necessarily in increasing order, and in many cases there are different values of y for the same value of x) which I can load into two row vectors of equal size.
I want to calculate the area enclosed by the curve.
How to do it with octave?
I tried this answer but it does not work because it seems that the area printed (204.64) is too high (see picture).
I tried the code:
function showdata(fName)
M = dlmread(fName);
H = M(2:end, 1); % starting row number is 2
B = M(2:end, 2);
aux = figure();
plot(H, B,'linewidth',2);
xlabel ("Auxilary field H (A/m)");
ylabel ("Magnetic Field B (Tesla)");
area = polyarea(H,B)
axis([min(H), max(H), min(B), max(B)]);
grid on;
grid minor on;
title (area,"fontsize",20);
Then I am calling showdata('data.txt') in Octave.
Picture of Data points:
This is the data file I am using.
There is a function for computing convex hull called "convhull" in Octave. It returns the indices of the points formming convex hull data.
M = dlmread("data.txt"); #I removed the header in data.txt
x = M(:,1);
y = M(:,2);
k = convhull(x,y);
plot (x(k), y(k), "r-", x, y, "b+");
n = rows(k);
x_prime = vertcat(x(k(n)), x(k(1:n-1)));
y_prime = vertcat(y(k(n)), y(k(1:n-1)));
A = .5*abs(x_prime'*y(k)-y_prime'*x(k)); #80.248
polyarea(x(k), y(k)) == A and true
Maybe convex hull is not good estimate of area because the top left and the down-right lines are a little far away from the points. There are other ways to form a polygon from data
, one of which could be alpha shape. However, alpha shape are more complicated and there is no corresponding pre-built function in Octave.
Update:
Each x corresponds to at least one y cordinate. I marked the highest point and lowest point laying on the same x and estimate the area again.
There is the code:
[uni, ~] = sort(unique(x));
n = rows(uni);
outline = [];
for i = 1:n
y_list = y(x==uni(i));
[y_max, ~] = max(y_list);
outline(i, :)= [uni(i), y_max];
[y_min, ~] = min(y_list);
outline(2*n-i+1,:)= [uni(i), y_min];
endfor
figure;
plot (x(k), y(k), "r-", x, y, "b+", outline(:,1), outline(:,2), "g-", "linewidth", 3);
polyarea(outline(:,1), outline(:,2)) #74.856
By the way, if the arguments of function polyarea do not form a close curve function polyarea would return wrong area.
Four point on a unit square:
[(0,0), (1,0), (1,1), (0,1)], [(0,0), (1,1), (1,0), (0,1)]
polyarea([0,1,1,0],[0,0,1,1])!==polyarea([0,1,1,0],[0,1,0,1]).

flash/actionscript 3.0 how to logically partition an image

I'm new to flash and I'm trying to create a board game with actionscript 3.0
I have already created the background (checker squares) for the board and now I have to partition the background by each box. What are the ways I can achieve that? I want to logically put numbers for each square as seen in the picture.
I realized its possible to do it using lasso tool and convert each to symbols. But is there any "lazy" way of doing that? There are lots of cuts I'd have to make in order to do that.
We can use some simple calculations to map some (x, y) value to a number. Lets say:
widht = width of the image
height = height of the image
gridCount = 8
gridWidth = width / gridCount
gridHeight = height / gridCount
Now first we would like to map user click point (x, y) to some integer index i, j to the logical 8 x 8 matrix where top left is index 0, 0.
i = x / gridWidth
j = y / gridHeight
For example, if gridWidth = 60, gridHeight = 50 and user clicks on (10, 15) then i = 0, j = 0.
Now we have to map this i, j to the specified numbers. As bottom line contains 11, 21, 31, ... and every column is increasing, the final number will be:
num = (11 + i * 10) + (gridCount - j - 1)
Converting these equations to AS3 code is straight forward, so I'm not adding them.

Total number of possible color combinations

I calculated there to be 16,777,216 possible hex color code combinations.
The maximum possible characters that we can have in a single hexadecimal character is 16 and the maximum possible characters a hex color code can contain is 6, and this brought me to my conclusion of 16^6.
Is this correct? If not, please tell me how many possible color combinations there are and how it can be worked out.
There are 16,777,216 colors using #RRGGBB notation.
Each color channel is described using 1 byte of information. Byte can contain 256 different values. So for 3 channels, it's:
256^3 = 16,777,216 = 16M
However, modern browsers support transparency - #AARRGGBB, by similar logic you get:
256^4 = 4,294,967,296 = 4G
yes it's true I make a simple node program return an array of all the possible hex code here is the code
function getColors(){
var hexCode = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E' ,'F'];
var arr = [];
for (var i = 0; i < hexCode.length; i++) {
console.log(`i done it ${i+1} times`);
for (var y = 0; y < hexCode.length; y++) {
for (var x = 0; x < hexCode.length; x++) {
for (var a = 0; a < hexCode.length; a++) {
for (var b = 0; b < hexCode.length; b++) {
for (var c = 0; c < hexCode.length; c++) {
arr.push(`#${hexCode[i]}${hexCode[y]}${hexCode[x]}${hexCode[a]}${hexCode[b]}${hexCode[c]}\n`);
}
}
}
}
}
}
return arr;
}
var colors = getColors();
console.log(colors.length);
However when I run it, it logs to the console : 16 777 216.
There are currently 184,549,376 possible color combinations in the rgba() color system, which is
R: 0 to 255 (256 values) ×
G: 0 to 255 (256 values) ×
B: 0 to 255 (256 values) ×
A: 0.0 to 1.0 (11 values)
There are 2 ways to write a color. RGB (rgb(R,G,B)) which has a range of 0-255 for red, green and blue. The Second way is hexadecimal (#RRGGBB).
In Hexadecimal there are 6 digits in total with 2 digits for each color. The maximum 2 digit value in hexadecimal is FF which in base 10 is 255.
If you think about it. RGB and HEX are similar in a way that allows you to enter 3 numbers for a Red, Green and Blue value. And the maximum value for each number is 255.
The maximum value for 6 hexadecimal digits in base 10 is 16,777,215. If you also add #000000 you get 16,777,216 as the total number of possible color combinations.
If we use RGB, the range of colors is 0-255. Meaning there are 256 possible values for each Red, Green and Blue. 256^3 is 16,777,216.
Therefore, the answer to your question is 16,777,216. No matter which way you count it.
Well, I think it's 16777216 to, because my hexadecimal converter said that ffffff was 16777215. ffffff is the highest hexadecimal color code, so that would make 16777215. However, there is also 000000, which makes the answer 16777216, since it did not include 000000.
Whoever typed that it was 16777216, you're right.

CanvasPixelArray - how many index spots for one color value?

For a personal project, I'm re-implementing some Javascript code to Java. One particular thing that is tripping me up at this point is whether a Color is represented by three or four index values in the HTML5 CanvasPixelArray object.
The page linked above states that an offset value of 4 is used. However, one graphic effect that I'm re-implementing has this function:
function getPixelValue(x, y) {
var offset = (x + y * width) * 4;
var r = imageData[offset];
var g = imageData[offset + 1];
var b = imageData[offset + 2];
return ( ((255 << 8) | r) << 8 | g) << 8 | b;
}
to return an color integer value for a given pixel. The code works in the browser, but I'm confused by the fact that r, g, b are all contained in a given 3 block segment of the array, while offset is 4. This same value for offset is shown in the code example at the page linked above.
What is the reason for the difference? If a pixel color value is contained within a 3 block segment, shouldn't offset include 3 as a constant?
Canvas always returns RGBA but you can skip the alpha channel (index 3) if you don't need it but will always have to skip 4 positions in the byte array.
Typically for photos the alpha value is always 255 (non-transparent) so it isn't needed. For other types of graphics which already contain an alpha channel (for example PNG icons etc.) the alpha channel becomes more important.
Your getPixelValue simply ignores the alpha channel and returns the RGB value independent on the value of the alpha channel (which is correct when you want a color value from the source - the color value (from source) will be the same regardless of the alpha value).

Best line equation to use for computational geometry

I'm looking to write a little comp-geom library, in Ruby.
I'm about to write the code for lines, and was wondering which line equation I should use:
ax + by + c = 0
r + tv (where r and v are vectors)
Thanks.
If using the classical equations is not a requirement, I'd suggest an array of four co-ordinates: xStart, yStart, xEnd and yEnd.
In case you need to make the line position dynamic, you could use an array of two parameters: alpha and radius. The former represents radial rotation relative to the horizontal axis and the latter is the length of line.
Yet another option would be vectors in the form of (X;Y).
Samples in C:
int endpointsLine[4] = {0, 0, 30, 40};
double radialLine[2] = {5.35589, 50};
int vectorLine[2] = {30, 40};
The "endpoints" format is fully compatible with modern line-drawing algorithms, such as Xiaolin Wu's line algorithm and Bresenham's line algorithm but it represents specific screen co-ordinates which is not the case with "radial" and "vector" format.