Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:40:23+00:00 2026-05-16T00:40:23+00:00

Hey all, I’m working on cleaning up my code from previous semesters. Previously I

  • 0

Hey all, I’m working on cleaning up my code from previous semesters.

Previously I created a 151 color swatch library in c++. However because of my time crunch and lack of experience, I created it entirely as a block of define statements. Which, for hard coding values into spots worked fine. However there are some obvious weaknesses to this approach.

What I have panned out so far, is to create a namespace ‘swatch’ and inside the namespace I would have an enumeration for the valid colors. I would also have a ‘getSwatch’ function, or something similar, that would return a vec3 (a class of mine, represents a vector of 3 elemets, with some nice functionality), and the function would use a switch statement to go through the valid swatches.

It would look something like this:

namespace swatch{

    enum color{
        red,
        blue,
        green
    }

    inline
    const vec3 getColor(const color& c){
        // Switch and return red blue or green.
    }
}

My Question: I’d like to know how you might suggest doing this? Benifits of preformance, and usability is what I’m most interested in.

Thanks in advance friends,

Happy coding.

Edit: I just changed the example to make more sense to people who don’t know how I use my vec class. (i.e: Everybody but me). Also, you can just look at the other anwsers for usage. They made a good guess on passing rgb values to the constructor, thats not how I did it, but I can still follow along just fine with what you mean.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:40:23+00:00Added an answer on May 16, 2026 at 12:40 am

    Use a lookup table:

    /************* .h *************/
    
    enum color{
        red,
        blue,
        green,
    
        colors_count
    }
    
    const vec3 &getColor(color c)
    {
        extern const vec3 colors_table[colors_count];
        return colors_table[c];
    }
    
    /************* .cpp *************/
    
    extern const vec3 colors_table[colors_count] = {
        vec3(255, 0, 0), // red
        vec3(0, 0, 255), // blue
        vec3(0, 255, 0), // green
    };
    

    You didn’t write anything about purpose of using templates so I just eliminated them. If you explain more then I will help maybe.

    // EDIT
    It’s very simple and very fast.
    In c++, enum values are not just some identifiers, they are numbers. If you don’t specify other they will be fallowing numbers starting from 0: ‘red’ is 0, ‘blue’ is 1, ‘green’ is 2 and ‘colors_count’ is 3 (see http://www.google.com/search?q=c%2B%2B+enum).
    You can use these numbers to index an array. Then you simply pick an item from an array at given index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm reviewing the IntelliJ ability related to JPA ER Diagrams (http://www.jetbrains.com/idea/features/jpa_hibernate.html), and
Hey all, i am trying to create a report to show how much is
Hey all , I want to put a flash file in the bottom of
Hey all, i am in need of some help trying to figure out why
Hey all, I'm writing a little 3D engine for android to better learn the
Hey all, I managed to integrate my Database quite well with Excel in the
Hey all. I am adding a More Rows button to a table within a
Hey all, I am creating a login script that in one step, enters the
Hey all, I've been struggling with a select statement for a bit now and
hEY ALL i have a table that shows transactions and their status. How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.