2

I am currently writing a color palette generator in Python. I would like to be able to input a Mode as an argument that will produce a specific type of color scheme. Like COMPLEMENTARY or PASTEL etc.

I have seen functions like colorMode(HSB) vs. colorMode(RGB) where one of the arguments is a "Mode". I have seen this many times before, they usually are all caps and the IDE usually color codes them in some way. How are these Modes usually stored within the function? Are they color coded because they are some kind of class? Or are they stored as a string or number?

Sorry if a question like this already exists, I just have no idea what type of jargon to use to describe this scenario.

3
  • 1
    Take a look at the enum library Commented Mar 13, 2018 at 15:51
  • @JamesElderfield Answer? Commented Mar 13, 2018 at 16:00
  • What do you mean color coded? IDEs color code most atoms, including variable names. Commented Mar 13, 2018 at 16:03

3 Answers 3

1

The other answers are correct for general development, but since you've tagged this with , I'll explain how Processing does it.

Processing (and Processing.py) does not use enum values for this. It just uses int values.

In Java (regular Processing), there are just a bunch of int variables defined. That happens here, but it basically looks like this:

int RGB = 1; int ARGB = 2; int HSB = 3; 

So whenever you use one of the variables, you're just refering to these values.

colorMode(HSB); 

This does the same thing as if you had typed:

colorMode(3); 

Then inside the colorMode() function, Processing does something like this:

void colorMode(int mode){ if(mode == RGB){ // do something } else if(mode == HSB){ // do something else } } 

I'm using Java as an example because I have more familiarity with it, but note that this is all explained in the Processing.py reference here:

mode int: Either RGB or HSB, corresponding to Red/Green/Blue and Hue/Saturation/Brightness

Notice that mode is an int.

If you're writing your own code, enum values are probably the way to go. But if you're asking about how Processing does it internally, then they use int values.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use the enum library. For example to create a "Mode" like you want you could do:

from enum import Enum class ColorMode(Enum): COMPLEMENTARY = 1 PASTEL = 2 

Afterwards you can access it using:

ColorMode.COMPLEMENTARY 

Comments

1

Use the enum module:

import enum class Mode(enum.Enum): COMPLEMENTARY = 1 PASTEL = 2 def colorMode(mode): return "using color mode {}".format(mode) if __name__ == "__main__": print(colorMode(Mode.COMPLEMENTARY)) print(colorMode(Mode.PASTEL)) 

Output:

using color mode Mode.COMPLEMENTARY using color mode Mode.PASTEL 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.