Loading...
Searching...
No Matches
colormath.h File Reference

SNES Color Math (Blending/Transparency) More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define COLDATA_ALL   (COLDATA_RED | COLDATA_GREEN | COLDATA_BLUE)
 Apply fixed color to all channels.
 
#define COLDATA_BLUE   0x80
 Apply fixed color to blue channel.
 
#define COLDATA_GREEN   0x40
 Apply fixed color to green channel.
 
#define COLDATA_RED   0x20
 Apply fixed color to red channel.
 
#define COLORMATH_ADD   0
 Add colors (brightens)
 
#define COLORMATH_ALL   0x3F
 Apply color math to all layers.
 
#define COLORMATH_ALWAYS   0
 Always enable color math.
 
#define COLORMATH_BACKDROP   BIT(5)
 Apply color math to backdrop (color 0)
 
#define COLORMATH_BG1   BIT(0)
 Apply color math to BG1.
 
#define COLORMATH_BG2   BIT(1)
 Apply color math to BG2.
 
#define COLORMATH_BG3   BIT(2)
 Apply color math to BG3.
 
#define COLORMATH_BG4   BIT(3)
 Apply color math to BG4.
 
#define COLORMATH_INSIDE   1
 Enable inside window only.
 
#define COLORMATH_NEVER   3
 Never enable color math.
 
#define COLORMATH_OBJ   BIT(4)
 Apply color math to sprites (OBJ)
 
#define COLORMATH_OUTSIDE   2
 Enable outside window only.
 
#define COLORMATH_SRC_FIXED   1
 Use fixed color as color source.
 
#define COLORMATH_SRC_SUBSCREEN   0
 Use sub screen as color source.
 
#define COLORMATH_SUB   1
 Subtract colors (darkens)
 

Functions

void colorMathDisable (void)
 Disable all color math.
 
void colorMathEnable (u8 layers)
 Enable color math for specified layers.
 
void colorMathInit (void)
 Initialize color math to defaults.
 
void colorMathSetBrightness (u8 brightness)
 Set brightness for fade effects.
 
void colorMathSetChannel (u8 channel, u8 intensity)
 Set a single fixed color channel.
 
void colorMathSetCondition (u8 condition)
 Set when color math is enabled (window control)
 
void colorMathSetFixedColor (u8 r, u8 g, u8 b)
 Set fixed color for blending.
 
void colorMathSetHalf (u8 enable)
 Enable or disable half mode.
 
void colorMathSetOp (u8 op)
 Set color math operation (add or subtract)
 
void colorMathSetSource (u8 source)
 Set color math source.
 
void colorMathShadow (u8 layers, u8 intensity)
 Set up shadow/darkening effect.
 
void colorMathTint (u8 layers, u8 r, u8 g, u8 b)
 Set up color tint effect.
 
void colorMathTransparency50 (u8 layers)
 Set up 50% transparency for layers.
 

Detailed Description

SNES Color Math (Blending/Transparency)

Color math enables blending between the main screen and sub screen or a fixed color. This creates effects like transparency, shadows, fading, and underwater tints.

How Color Math Works

The SNES can add or subtract colors:

  • Add: Main screen + Sub screen (or fixed color) = brighter
  • Subtract: Main screen - Sub screen (or fixed color) = darker

Half mode divides the result by 2, useful for 50% transparency.

Main Screen vs Sub Screen

  • Main screen: Primary display (what you normally see)
  • Sub screen: Secondary layer used for blending

For transparency, put the background on main screen and the transparent layer on sub screen.

Usage Example

// 50% transparent BG2 over BG1
REG_TM = TM_BG1 | TM_BG2; // Both on main screen
REG_TS = TM_BG2; // BG2 also on sub screen
colorMathEnable(COLORMATH_BG2); // Apply math to BG2
colorMathSetHalf(1); // Divide by 2 = 50%
colorMathSetSource(COLORMATH_SRC_SUBSCREEN); // Blend with sub screen
#define COLORMATH_SRC_SUBSCREEN
Use sub screen as color source.
Definition colormath.h:95
#define COLORMATH_ADD
Add colors (brightens)
Definition colormath.h:85
#define COLORMATH_BG2
Apply color math to BG2.
Definition colormath.h:63
void colorMathEnable(u8 layers)
Enable color math for specified layers.
void colorMathSetHalf(u8 enable)
Enable or disable half mode.
void colorMathSetOp(u8 op)
Set color math operation (add or subtract)
void colorMathSetSource(u8 source)
Set color math source.
#define REG_TS
Sub screen designation (W)
Definition registers.h:184
#define REG_TM
Main screen designation (W)
Definition registers.h:181
#define TM_BG1
Definition registers.h:439
#define TM_BG2
Definition registers.h:440
// Fade to black
colorMathSetFixedColor(0, 0, 0); // Black
colorMathSetOp(COLORMATH_SUB); // Subtract = darken
colorMathEnable(COLORMATH_ALL); // Apply to all layers
#define COLORMATH_ALL
Apply color math to all layers.
Definition colormath.h:78
void colorMathSetFixedColor(u8 r, u8 g, u8 b)
Set fixed color for blending.
#define COLORMATH_SRC_FIXED
Use fixed color as color source.
Definition colormath.h:98
#define COLORMATH_SUB
Subtract colors (darkens)
Definition colormath.h:88
Author
OpenSNES Team

Macro Definition Documentation

◆ COLDATA_ALL

#define COLDATA_ALL   (COLDATA_RED | COLDATA_GREEN | COLDATA_BLUE)

Apply fixed color to all channels.

◆ COLDATA_BLUE

#define COLDATA_BLUE   0x80

Apply fixed color to blue channel.

◆ COLDATA_GREEN

#define COLDATA_GREEN   0x40

Apply fixed color to green channel.

◆ COLDATA_RED

#define COLDATA_RED   0x20

Apply fixed color to red channel.

◆ COLORMATH_ADD

#define COLORMATH_ADD   0

Add colors (brightens)

◆ COLORMATH_ALL

#define COLORMATH_ALL   0x3F

Apply color math to all layers.

◆ COLORMATH_ALWAYS

#define COLORMATH_ALWAYS   0

Always enable color math.

◆ COLORMATH_BACKDROP

#define COLORMATH_BACKDROP   BIT(5)

Apply color math to backdrop (color 0)

◆ COLORMATH_BG1

#define COLORMATH_BG1   BIT(0)

Apply color math to BG1.

◆ COLORMATH_BG2

#define COLORMATH_BG2   BIT(1)

Apply color math to BG2.

◆ COLORMATH_BG3

#define COLORMATH_BG3   BIT(2)

Apply color math to BG3.

◆ COLORMATH_BG4

#define COLORMATH_BG4   BIT(3)

Apply color math to BG4.

◆ COLORMATH_INSIDE

#define COLORMATH_INSIDE   1

Enable inside window only.

◆ COLORMATH_NEVER

#define COLORMATH_NEVER   3

Never enable color math.

◆ COLORMATH_OBJ

#define COLORMATH_OBJ   BIT(4)

Apply color math to sprites (OBJ)

◆ COLORMATH_OUTSIDE

#define COLORMATH_OUTSIDE   2

Enable outside window only.

◆ COLORMATH_SRC_FIXED

#define COLORMATH_SRC_FIXED   1

Use fixed color as color source.

◆ COLORMATH_SRC_SUBSCREEN

#define COLORMATH_SRC_SUBSCREEN   0

Use sub screen as color source.

◆ COLORMATH_SUB

#define COLORMATH_SUB   1

Subtract colors (darkens)

Function Documentation

◆ colorMathDisable()

void colorMathDisable ( void  )

Disable all color math.

◆ colorMathEnable()

void colorMathEnable ( u8  layers)

Enable color math for specified layers.

Enables color math blending for the given layers.

Parameters
layersLayer mask (COLORMATH_BG1, COLORMATH_BG2, etc.)

◆ colorMathInit()

void colorMathInit ( void  )

Initialize color math to defaults.

Disables all color math effects.

◆ colorMathSetBrightness()

void colorMathSetBrightness ( u8  brightness)

Set brightness for fade effects.

Sets the fixed color for use in fading. Use with colorMathSetOp() to fade to white (add) or black (subtract).

Parameters
brightnessFade level (0 = no effect, 31 = full white/black)

◆ colorMathSetChannel()

void colorMathSetChannel ( u8  channel,
u8  intensity 
)

Set a single fixed color channel.

Parameters
channelChannel mask (COLDATA_RED, COLDATA_GREEN, COLDATA_BLUE)
intensityIntensity (0-31)

◆ colorMathSetCondition()

void colorMathSetCondition ( u8  condition)

Set when color math is enabled (window control)

Parameters
conditionCOLORMATH_ALWAYS, COLORMATH_INSIDE, COLORMATH_OUTSIDE, or COLORMATH_NEVER

◆ colorMathSetFixedColor()

void colorMathSetFixedColor ( u8  r,
u8  g,
u8  b 
)

Set fixed color for blending.

Sets the fixed color used when source is COLORMATH_SRC_FIXED.

Parameters
rRed intensity (0-31)
gGreen intensity (0-31)
bBlue intensity (0-31)

◆ colorMathSetHalf()

void colorMathSetHalf ( u8  enable)

Enable or disable half mode.

When enabled, the color math result is divided by 2. This creates 50% transparency/blending.

Parameters
enable1 = divide by 2, 0 = full result

◆ colorMathSetOp()

void colorMathSetOp ( u8  op)

Set color math operation (add or subtract)

Parameters
opOperation (COLORMATH_ADD or COLORMATH_SUB)

◆ colorMathSetSource()

void colorMathSetSource ( u8  source)

Set color math source.

Chooses whether to blend with the sub screen or a fixed color.

Parameters
sourceCOLORMATH_SRC_SUBSCREEN or COLORMATH_SRC_FIXED

◆ colorMathShadow()

void colorMathShadow ( u8  layers,
u8  intensity 
)

Set up shadow/darkening effect.

Subtracts fixed color from layers to darken them.

Parameters
layersLayers to darken
intensityDarkness level (0-31, higher = darker)

◆ colorMathTint()

void colorMathTint ( u8  layers,
u8  r,
u8  g,
u8  b 
)

Set up color tint effect.

Adds a fixed color tint to layers.

Parameters
layersLayers to tint
rRed tint (0-31)
gGreen tint (0-31)
bBlue tint (0-31)

◆ colorMathTransparency50()

void colorMathTransparency50 ( u8  layers)

Set up 50% transparency for layers.

Quick setup for semi-transparent layers. The transparent layers should be on both main and sub screen.

Parameters
layersLayers to make 50% transparent
Note
You must also set REG_TS to include the transparent layers