SFML community forums

Help => Graphics => Topic started by: Ashenwraith on April 12, 2010, 09:53:04 pm

Title: [solved]SFML Color probs
Post by: Ashenwraith on April 12, 2010, 09:53:04 pm
Quote from: "Ashenwraith"
Hi, here's an example of my probs, any help would be great:

Code: [Select]
sf::Color c1,c2;

c1=img.GetPixel(0,0);

if(c1!=c2) // doesn't compare colors
{

}


Nevermind, I forgot another check.
Title: [solved]SFML Color probs
Post by: panithadrum on April 12, 2010, 10:04:31 pm
What do you mean? The compare method works fine.
Title: [solved]SFML Color probs
Post by: Ashenwraith on April 12, 2010, 10:11:05 pm
Quote from: "panithadrum"
What do you mean? The compare method works fine.


Yeah, I put in a simple 4 color image to check it, but I forgot to check if the color switched back on the next swap.
Title: [solved]SFML Color probs
Post by: Ashenwraith on April 12, 2010, 11:35:28 pm
Quote from: "panithadrum"
What do you mean? The compare method works fine.


hmmm... still having probs with it .

Are you sure this works for comparing the r,g,b,a value?

What are the bool operators for?

bool sf::Color::operator!=  ( const Color &  Other   )  const


I can't get these to work either.
Title: [solved]SFML Color probs
Post by: Spidyy on April 12, 2010, 11:42:32 pm
From SFML source code :
Code: [Select]
////////////////////////////////////////////////////////////
bool operator ==(const Color& left, const Color& right)
{
    return (left.r == right.r) &&
           (left.g == right.g) &&
           (left.b == right.b) &&
           (left.a == right.a);
}


////////////////////////////////////////////////////////////
bool operator !=(const Color& left, const Color& right)
{
    return !(left == right);
}


It mean what is write. =p Are you sure you compare the right color, including alpha channel?
Title: [solved]SFML Color probs
Post by: Ashenwraith on April 12, 2010, 11:55:14 pm
yeah, yeah.. I know

I don't know what's wrong with my code.

It would help if there was a way to print all these variables.

It's hard to tell if they are working.
Title: [solved]SFML Color probs
Post by: panithadrum on April 12, 2010, 11:57:57 pm
Quote from: "Ashenwraith"
yeah, yeah.. I know

I don't know what's wrong with my code.

It would help if there was a way to print all these variables.

It's hard to tell if they are working.


Code: [Select]

void PrintColor(const sf::Color &Color)
{
    std::cout << Color.r << ", " << Color.g << ", " << Color.b << ", " << Color.a << std::endl;
}
Title: [solved]SFML Color probs
Post by: Ashenwraith on April 13, 2010, 12:11:27 am
Quote from: panithadrum
Quote from: "Ashenwraith"
yeah, yeah.. I know



Code: [Select]

void PrintColor(const sf::Color &Color)
{
    std::cout << Color.r << ", " << Color.g << ", " << Color.b << ", " << Color.a << std::endl;
}


Thanks, I got it working.

hmmm... I tried
Code: [Select]

std::cout<<my_color.a<<'\n';


But it was only blank.

Why does your code work?

I think it might be the windows console, I get strange characters for a lot of these cout.
Title: [solved]SFML Color probs
Post by: panithadrum on April 13, 2010, 12:30:53 am
Hmm.... I think you should show some relevant code. I'm kinda lost :-D
Title: [solved]SFML Color probs
Post by: Spidyy on April 13, 2010, 12:35:05 am
Keep the \n for the printf thing, use std::endl with the std::cout instead. :p

And I agree, show some codes. It's easier to help you while seeing your code (and your mistakes) that without. :p
Title: [solved]SFML Color probs
Post by: nulloid on April 13, 2010, 12:56:17 am
Quote from: "Ashenwraith"

I think it might be the windows console, I get strange characters for a lot of these cout.


Or maybe you just need to write <<"\n" instead of <<'\n', or at least '\n\0'. C(++) uses null-terminated strings. ("string" means string + \0)
Title: [solved]SFML Color probs
Post by: Nexus on April 13, 2010, 01:17:54 am
Quote from: "Ashenwraith"
hmmm... I tried
Code: [Select]
std::cout<<my_color.a<<'\n';But it was only blank.
The sf::Color members are of type sf::Uint8, which is a typedef for unsigned char. And the unsigned char overload of std::ostream::operator<< tries to write characters, not numbers. So, you have to cast to a type that is recognized by the streams as a number:
Code: [Select]
std::cout << static_cast<int>(my_color.a) << std::endl;

Quote from: "nulloid"
Or maybe you just need to write <<"\n" instead of <<'\n', or at least '\n\0'.
No, that's not the problem. std::cout can handle single characters. And '\n\0' is not a valid char literal, you can't store two characters in one byte.
Title: [solved]SFML Color probs
Post by: Ashenwraith on April 13, 2010, 09:13:02 am
Quote from: "Nexus"
Quote from: "Ashenwraith"
hmmm... I tried
Code: [Select]
std::cout<<my_color.a<<'\n';But it was only blank.
The sf::Color members are of type sf::Uint8, which is a typedef for unsigned char. And the unsigned char overload of std::ostream::operator<< tries to write characters, not numbers. So, you have to cast to a type that is recognized by the streams as a number:
Code: [Select]
std::cout << static_cast<int>(my_color.a) << std::endl;

Quote from: "nulloid"
Or maybe you just need to write <<"\n" instead of <<'\n', or at least '\n\0'.
No, that's not the problem. std::cout can handle single characters. And '\n\0' is not a valid char literal, you can't store two characters in one byte.


Hey thanks with the cast help. That's what I thought it was. I haven't used C++ in a while and this new syntax looks good without having to use a lib like boost. I'll have to check to see if it works with wchar_t and other stuff I'm working with.