Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using named colors  (Read 10965 times)

0 Members and 1 Guest are viewing this topic.

Silvah

  • Guest
Using named colors
« Reply #15 on: October 14, 2010, 11:41:16 am »
Quote from: "Nexus"
"So I don't construct constants and use bad code instead"
Can you explain me why creating objects at local scope and only when they're really needed is considered "bad code"?

I'm not talking now about the problem in this thread, but rather about its generalization. Why
Code: [Select]
void SomeClass::foo()
{
  // we don't need this constant anywhere except in this function
  const Something blah(888);
  // ... some code ...
}
is worse than
Code: [Select]
// literally hundreds of non-trivial global constants omitted
void SomeClass::foo()
{
  // ... some code ...
}
? I always thought the opposite is true.

Quote from: "Nexus"
You don't know why the browser starts slowly. You don't know either how long the construction takes and how relevant it really is, because you have never measured it. You are just guessing.
Uhm, okay, you got me this time. I don't even want to know why that browser starts so slowly, it's simplier to switch to a different one.

But I know why one of programs that I was working on was starting slowly. Unless the profiler was just a random number generator in disguise, the global constructors (and stupid fellow programmers - how stupid one has to be to perform a lot of I/O in a constructor of global object (moreover, it was a constant!) and to make just about everything global?) were guilt.

Quote from: "Nexus"
It's just bad style to evoke undefined behaviour (union-cast) because of a non-existing problem.
As Laurent showed us, it's perfectly possible to get rid of union cast in this case.

Quote from: "Nexus"
The one who told you that probably meant constructions of depending modules shouldn't happen at global level since the initialization order is undefined.
The real reason was that sometimes they were not called, since the runtime never supported them properly.

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Using named colors
« Reply #16 on: October 14, 2010, 01:01:52 pm »
Finally I created a structure containing static constants. From now on I'm using it and quite happy.  :)  For those who want the constants here are the code. For portability /#pragma once/ should be replaced with an include guard.

header:

Code: [Select]
#pragma once

#include <SFML/Graphics/Color.hpp>

namespace Named
{

//-----------------------------------------------------------------------------
struct Color
{
    static const sf::Color AliceBlue;
    static const sf::Color AntiqueWhite;
    static const sf::Color Aqua;          
    static const sf::Color Aquamarine;    
    static const sf::Color Azure;          
    static const sf::Color Beige;          
    static const sf::Color Bisque;        
    static const sf::Color Black;          
    static const sf::Color BlanchedAlmond;
    static const sf::Color Blue;          
    static const sf::Color BlueViolet;    
    static const sf::Color Brown;          
    static const sf::Color Burlywood;      
    static const sf::Color CadetBlue;      
    static const sf::Color Chartreuse;    
    static const sf::Color Chocolate;      
    static const sf::Color Coral;          
    static const sf::Color CornflowerBlue;
    static const sf::Color Cornsilk;      
    static const sf::Color Crimson;        
    static const sf::Color Cyan;          
    static const sf::Color DarkBlue;      
    static const sf::Color DarkCyan;      
    static const sf::Color DarkGoldenrod;  
    static const sf::Color DarkGray;      
    static const sf::Color DarkGreen;      
    static const sf::Color DarkKhaki;      
    static const sf::Color DarkMagenta;    
    static const sf::Color DarkOlivegreen;
    static const sf::Color DarkOrange;    
    static const sf::Color DarkOrchid;    
    static const sf::Color DarkRed;        
    static const sf::Color DarkSalmon;    
    static const sf::Color DarkSeagreen;  
    static const sf::Color DarkSlateblue;  
    static const sf::Color DarkSlategray;  
    static const sf::Color DarkTurquoise;  
    static const sf::Color DarkViolet;    
    static const sf::Color DeepPink;      
    static const sf::Color DeepSkyblue;    
    static const sf::Color DimGray;        
    static const sf::Color DodgerBlue;    
    static const sf::Color FireBrick;      
    static const sf::Color FloralWhite;    
    static const sf::Color ForestGreen;    
    static const sf::Color Fuchsia;
    static const sf::Color Gainsboro;            
    static const sf::Color GhostWhite;          
    static const sf::Color Gold;                
    static const sf::Color Goldenrod;            
    static const sf::Color Gray;                
    static const sf::Color Green;                
    static const sf::Color GreenYellow;          
    static const sf::Color Honeydew;            
    static const sf::Color HotPink;              
    static const sf::Color IndianRed;            
    static const sf::Color Indigo;              
    static const sf::Color Ivory;                
    static const sf::Color Khaki;                
    static const sf::Color Lavender;            
    static const sf::Color LavenderBlush;        
    static const sf::Color LawnGreen;            
    static const sf::Color LemonChiffon;        
    static const sf::Color LightBlue;            
    static const sf::Color LightCoral;          
    static const sf::Color LightCyan;            
    static const sf::Color LightGoldenrodyellow;
    static const sf::Color LightGray;            
    static const sf::Color LightGreen;          
    static const sf::Color LightPink;            
    static const sf::Color LightSalmon;          
    static const sf::Color LightSeagreen;        
    static const sf::Color LightSkyblue;        
    static const sf::Color LightSlategray;      
    static const sf::Color LightSteelblue;      
    static const sf::Color LightYellow;          
    static const sf::Color Lime;                
    static const sf::Color LimeGreen;            
    static const sf::Color Linen;                
    static const sf::Color Magenta;              
    static const sf::Color Maroon;              
    static const sf::Color MediumAquamarine;    
    static const sf::Color MediumBlue;          
    static const sf::Color MediumOrchid;        
    static const sf::Color MediumPurple;        
    static const sf::Color MediumSeaGreen;      
    static const sf::Color MediumSlateBlue;      
    static const sf::Color MediumSpringGreen;    
    static const sf::Color MediumTurquoise;      
    static const sf::Color MediumBioletRed;      
    static const sf::Color MidnightBlue;        
    static const sf::Color MintCream;
    static const sf::Color MistyRose;    
    static const sf::Color Moccasin;      
    static const sf::Color NavajoWhite;  
    static const sf::Color Navy;          
    static const sf::Color OldLace;      
    static const sf::Color Olive;        
    static const sf::Color OliveDrab;    
    static const sf::Color Orange;        
    static const sf::Color OrangeRed;    
    static const sf::Color Orchid;        
    static const sf::Color PaleGoldenrod;
    static const sf::Color PaleGreen;    
    static const sf::Color PaleTurquoise;
    static const sf::Color PaleVioletRed;
    static const sf::Color PapayaWhip;    
    static const sf::Color PeachPuff;    
    static const sf::Color Peru;          
    static const sf::Color Pink;          
    static const sf::Color Plum;          
    static const sf::Color PowderBlue;    
    static const sf::Color Purple;        
    static const sf::Color Red;          
    static const sf::Color RosyBrown;    
    static const sf::Color RoyalBlue;    
    static const sf::Color SaddleBrown;  
    static const sf::Color Salmon;        
    static const sf::Color SandyBrown;    
    static const sf::Color SeaGreen;      
    static const sf::Color SeaShell;      
    static const sf::Color Sienna;        
    static const sf::Color Silver;        
    static const sf::Color SkyBlue;      
    static const sf::Color SlateBlue;    
    static const sf::Color SlateGray;    
    static const sf::Color Snow;          
    static const sf::Color SpringGreen;  
    static const sf::Color SteelBlue;    
    static const sf::Color Tan;          
    static const sf::Color Teal;          
    static const sf::Color Thistle;      
    static const sf::Color Tomato;        
    static const sf::Color Turquoise;    
    static const sf::Color Violet;        
    static const sf::Color Wheat;        
    static const sf::Color White;        
    static const sf::Color WhiteSmoke;    
    static const sf::Color Yellow;        
    static const sf::Color YellowGreen;    
};

} // namespace Named


implementation:

Code: [Select]
namespace Named
{

    const sf::Color Color::AliceBlue(0xF0, 0xF8, 0xFF);
    const sf::Color Color::AntiqueWhite(0xFA, 0xEB, 0xD7);
    const sf::Color Color::Aqua(0x00, 0xFF, 0xFF);
    const sf::Color Color::Aquamarine(0x7F, 0xFF, 0xD4);
    const sf::Color Color::Azure(0xF0, 0xFF, 0xFF);
    const sf::Color Color::Beige(0xF5, 0xF5, 0xDC);    
    const sf::Color Color::Bisque(0xFF, 0xE4, 0xC4);
    const sf::Color Color::Black(0x00, 0x00, 0x00);
    const sf::Color Color::BlanchedAlmond(0xFF, 0xEB, 0xCD);
    const sf::Color Color::Blue(0x00, 0x00, 0xFF);
    const sf::Color Color::BlueViolet(0x8A, 0x2B, 0xE2);
    const sf::Color Color::Brown(0xA5, 0x2A, 0x2A);    
    const sf::Color Color::Burlywood(0xDE, 0xB8, 0x87);
    const sf::Color Color::CadetBlue(0x5F, 0x9E, 0xA0);
    const sf::Color Color::Chartreuse(0x7F, 0xFF, 0x00);
    const sf::Color Color::Chocolate(0xD2, 0x69, 0x1E);
    const sf::Color Color::Coral(0xFF, 0x7F, 0x50);
    const sf::Color Color::CornflowerBlue(0x64, 0x95, 0xED);
    const sf::Color Color::Cornsilk(0xFF, 0xF8, 0xDC);
    const sf::Color Color::Crimson(0xDC, 0x14, 0x3C);
    const sf::Color Color::Cyan(0x00, 0xFF, 0xFF);
    const sf::Color Color::DarkBlue(0x00, 0x00, 0x8B);
    const sf::Color Color::DarkCyan(0x00, 0x8B, 0x8B);
    const sf::Color Color::DarkGoldenrod(0xB8, 0x86, 0x0B);
    const sf::Color Color::DarkGray(0xA9, 0xA9, 0xA9);
    const sf::Color Color::DarkGreen(0x00, 0x64, 0x00);
    const sf::Color Color::DarkKhaki(0xBD, 0xB7, 0x6B);
    const sf::Color Color::DarkMagenta(0x8B, 0x00, 0x8B);
    const sf::Color Color::DarkOlivegreen(0x55, 0x6B, 0x2F);
    const sf::Color Color::DarkOrange(0xFF, 0x8C, 0x00);
    const sf::Color Color::DarkOrchid(0x99, 0x32, 0xCC);
    const sf::Color Color::DarkRed(0x8B, 0x00, 0x00);
    const sf::Color Color::DarkSalmon(0xE9, 0x96, 0x7A);
    const sf::Color Color::DarkSeagreen(0x8F, 0xBC, 0x8F);
    const sf::Color Color::DarkSlateblue(0x48, 0x3D, 0x8B);
    const sf::Color Color::DarkSlategray(0x2F, 0x4F, 0x4F);
    const sf::Color Color::DarkTurquoise(0x00, 0xCE, 0xD1);
    const sf::Color Color::DarkViolet(0x94, 0x00, 0xD3);
    const sf::Color Color::DeepPink(0xFF, 0x14, 0x93);
    const sf::Color Color::DeepSkyblue(0x00, 0xBF, 0xFF);
    const sf::Color Color::DimGray(0x69, 0x69, 0x69);
    const sf::Color Color::DodgerBlue(0x1E, 0x90, 0xFF);
    const sf::Color Color::FireBrick(0xB2, 0x22, 0x22);
    const sf::Color Color::FloralWhite(0xFF, 0xFA, 0xF0);
    const sf::Color Color::ForestGreen(0x22, 0x8B, 0x22);
    const sf::Color Color::Fuchsia(0xFF, 0x00, 0xFF);
    const sf::Color Color::Gainsboro(0xDC, 0xDC, 0xDC);
    const sf::Color Color::GhostWhite(0xF8, 0xF8, 0xFF);
    const sf::Color Color::Gold(0xFF, 0xD7, 0x00);
    const sf::Color Color::Goldenrod(0xDA, 0xA5, 0x20);
    const sf::Color Color::Gray(0x80, 0x80, 0x80);
    const sf::Color Color::Green(0x00, 0x80, 0x00);
    const sf::Color Color::GreenYellow(0xAD, 0xFF, 0x2F);
    const sf::Color Color::Honeydew(0xF0, 0xFF, 0xF0);
    const sf::Color Color::HotPink(0xFF, 0x69, 0xB4);
    const sf::Color Color::IndianRed(0xCD, 0x5C, 0x5C);
    const sf::Color Color::Indigo(0x4B, 0x00, 0x82);
    const sf::Color Color::Ivory(0xFF, 0xFF, 0xF0);
    const sf::Color Color::Khaki(0xF0, 0xE6, 0x8C);
    const sf::Color Color::Lavender(0xE6, 0xE6, 0xFA);
    const sf::Color Color::LavenderBlush(0xFF, 0xF0, 0xF5);
    const sf::Color Color::LawnGreen(0x7C, 0xFC, 0x00);
    const sf::Color Color::LemonChiffon(0xFF, 0xFA, 0xCD);
    const sf::Color Color::LightBlue(0xAD, 0xD8, 0xE6);
    const sf::Color Color::LightCoral(0xF0, 0x80, 0x80);
    const sf::Color Color::LightCyan(0xE0, 0xFF, 0xFF);
    const sf::Color Color::LightGoldenrodyellow(0xFA, 0xFA, 0xD2);
    const sf::Color Color::LightGray(0xD3, 0xD3, 0xD3);
    const sf::Color Color::LightGreen(0x90, 0xEE, 0x90);
    const sf::Color Color::LightPink(0xFF, 0xB6, 0xC1);
    const sf::Color Color::LightSalmon(0xFF, 0xA0, 0x7A);
    const sf::Color Color::LightSeagreen(0x20, 0xB2, 0xAA);
    const sf::Color Color::LightSkyblue(0x87, 0xCE, 0xFA);
    const sf::Color Color::LightSlategray(0x77, 0x88, 0x99);
    const sf::Color Color::LightSteelblue(0xB0, 0xC4, 0xDE);
    const sf::Color Color::LightYellow(0xFF, 0xFF, 0xE0);
    const sf::Color Color::Lime(0x00, 0xFF, 0x00);
    const sf::Color Color::LimeGreen(0x32, 0xCD, 0x32);
    const sf::Color Color::Linen(0xFA, 0xF0, 0xE6);
    const sf::Color Color::Magenta(0xFF, 0x00, 0xFF);
    const sf::Color Color::Maroon(0x80, 0x00, 0x00);
    const sf::Color Color::MediumAquamarine(0x66, 0xCD, 0xAA);
    const sf::Color Color::MediumBlue(0x00, 0x00, 0xCD);
    const sf::Color Color::MediumOrchid(0xBA, 0x55, 0xD3);
    const sf::Color Color::MediumPurple(0x93, 0x70, 0xDB);
    const sf::Color Color::MediumSeaGreen(0x3C, 0xB3, 0x71);
    const sf::Color Color::MediumSlateBlue(0x7B, 0x68, 0xEE);
    const sf::Color Color::MediumSpringGreen(0x00, 0xFA, 0x9A);
    const sf::Color Color::MediumTurquoise(0x48, 0xD1, 0xCC);
    const sf::Color Color::MediumBioletRed(0xC7, 0x15, 0x85);
    const sf::Color Color::MidnightBlue(0x19, 0x19, 0x70);
    const sf::Color Color::MintCream(0xF5, 0xFF, 0xFA);
    const sf::Color Color::MistyRose(0xFF, 0xE4, 0xE1);
    const sf::Color Color::Moccasin(0xFF, 0xE4, 0xB5);
    const sf::Color Color::NavajoWhite(0xFF, 0xDE, 0xAD);
    const sf::Color Color::Navy(0x00, 0x00, 0x80);
    const sf::Color Color::OldLace(0xFD, 0xF5, 0xE6);
    const sf::Color Color::Olive(0x80, 0x80, 0x00);
    const sf::Color Color::OliveDrab(0x6B, 0x8E, 0x23);
    const sf::Color Color::Orange(0xFF, 0xA5, 0x00);
    const sf::Color Color::OrangeRed(0xFF, 0x45, 0x00);
    const sf::Color Color::Orchid(0xDA, 0x70, 0xD6);
    const sf::Color Color::PaleGoldenrod(0xEE, 0xE8, 0xAA);
    const sf::Color Color::PaleGreen(0x98, 0xFB, 0x98);
    const sf::Color Color::PaleTurquoise(0xAF, 0xEE, 0xEE);
    const sf::Color Color::PaleVioletRed(0xDB, 0x70, 0x93);
    const sf::Color Color::PapayaWhip(0xFF, 0xEF, 0xD5);
    const sf::Color Color::PeachPuff(0xFF, 0xDA, 0xB9);
    const sf::Color Color::Peru(0xCD, 0x85, 0x3F);
    const sf::Color Color::Pink(0xFF, 0xC0, 0xCB);
    const sf::Color Color::Plum(0xDD, 0xA0, 0xDD);
    const sf::Color Color::PowderBlue(0xB0, 0xE0, 0xE6);
    const sf::Color Color::Purple(0x80, 0x00, 0x80);
    const sf::Color Color::Red(0xFF, 0x00, 0x00);
    const sf::Color Color::RosyBrown(0xBC, 0x8F, 0x8F);
    const sf::Color Color::RoyalBlue(0x41, 0x69, 0xE1);
    const sf::Color Color::SaddleBrown(0x8B, 0x45, 0x13);
    const sf::Color Color::Salmon(0xFA, 0x80, 0x72);
    const sf::Color Color::SandyBrown(0xF4, 0xA4, 0x60);
    const sf::Color Color::SeaGreen(0x2E, 0x8B, 0x57);
    const sf::Color Color::SeaShell(0xFF, 0xF5, 0xEE);
    const sf::Color Color::Sienna(0xA0, 0x52, 0x2D);
    const sf::Color Color::Silver(0xC0, 0xC0, 0xC0);
    const sf::Color Color::SkyBlue(0x87, 0xCE, 0xEB);
    const sf::Color Color::SlateBlue(0x6A, 0x5A, 0xCD);
    const sf::Color Color::SlateGray(0x70, 0x80, 0x90);
    const sf::Color Color::Snow(0xFF, 0xFA, 0xFA);
    const sf::Color Color::SpringGreen(0x00, 0xFF, 0x7F);
    const sf::Color Color::SteelBlue(0x46, 0x82, 0xB4);
    const sf::Color Color::Tan(0xD2, 0xB4, 0x8C);
    const sf::Color Color::Teal(0x00, 0x80, 0x80);
    const sf::Color Color::Thistle(0xD8, 0xBF, 0xD8);
    const sf::Color Color::Tomato(0xFF, 0x63, 0x47);
    const sf::Color Color::Turquoise(0x40, 0xE0, 0xD0);
    const sf::Color Color::Violet(0xEE, 0x82, 0xEE);
    const sf::Color Color::Wheat(0xF5, 0xDE, 0xB3);
    const sf::Color Color::White(0xFF, 0xFF, 0xFF);
    const sf::Color Color::WhiteSmoke(0xF5, 0xF5, 0xF5);
    const sf::Color Color::Yellow(0xFF, 0xFF, 0x00);
    const sf::Color Color::YellowGreen(0x9A, 0xCD, 0x32);

} // namespace Named


using:

Code: [Select]
   label.SetTextColor(Named::Color::Beige);
    label.SetFrameColor(Named::Color::Crimson);


Who likes functions can get rid of union convertion and use the function and enum from my initial post. Others can use the structure. Who does not care, can forget about this topic at all.     :)

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Using named colors
« Reply #17 on: October 14, 2010, 01:17:09 pm »
Now it looks neat :-)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Using named colors
« Reply #18 on: October 14, 2010, 05:25:03 pm »
Quote from: "Silvah"
I always thought the opposite is true.
You are right, keeping things local is good practice. The "bad code" referred to the undefined behaviour (using union) and higher code complexity.

Quote from: "Silvah"
As Laurent showed us, it's perfectly possible to get rid of union cast in this case.
Yes, but then your main argument concerning the additional speed doesn't apply anymore.

Quote from: "Silvah"
But I know why one of programs that I was working on was starting slowly. Unless the profiler was just a random number generator in disguise, the global constructors (and stupid fellow programmers - how stupid one has to be to perform a lot of I/O in a constructor of global object (moreover, it was a constant!) and to make just about everything global?) were guilt.
[...]
The real reason was that sometimes they were not called, since the runtime never supported them properly.
Okay, but lets be honest. ;)
In these examples, the problem is definitely not comparable to the optimization approach in this thread. It's an interesting anectode anyway. :)

Quote from: "Relic"
Finally I created a structure containing static constants.
Why do you take a struct instead of a namespace? I don't think you ever want to instanciate it. And why do you prefer Named::Color::ActualColor over NamedColor::ActualColor?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
Using named colors
« Reply #19 on: October 14, 2010, 06:24:17 pm »
Quote from: "Nexus"
Yes, but then your main argument concerning the additional speed doesn't apply anymore.
Neither it applied to the original OP's code. Or it did and still does - it depends upon whether we're talking about startup speed or the "proper" runtime speed.

Quote from: "Nexus"
In these examples, the problem is definitely not comparable to the optimization approach in this thread.
Yeah, more than thousand movs and more than a hundred of call/ret pairs (that's what OP's code with constants boils down to) are definitely not comparable with doing I/O before main. ;)


Tiny question to Laurent: why constructors of sf::Color are not inline?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Using named colors
« Reply #20 on: October 14, 2010, 06:32:14 pm »
Quote
why constructors of sf::Color are not inline?

Because I never inline member functions by default. I do when profilers or users experiences prove that it is really necessary. My rule is "write clean code first, then optimize if necessary" -- yes, I find that putting implementation details in a header is not clean ;)
Laurent Gomila - SFML developer

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Using named colors
« Reply #21 on: October 15, 2010, 05:20:26 am »
Quote
Why do you take a struct instead of a namespace?

I'm gonna add some methods to it later.

 

anything