Log In  

Cart #colorspace_generator-0 | 2024-03-24 | Embed ▽ | License: CC4-BY-NC-SA
3

Generates a colorspace graphic for the currently loaded palette, and generates colortables for precomputed color blending based on that colorspace.

Supports palettes in hex format. Just drop the palette onto the window to load it before generating the colorspace.
The colorspace is just a gfx file, so it can be opened in picotron's gfx editor.

There are four export modes:

  • Colorspace + Tables - exports the colorspace gfx with all of the currently generated color tables embedded. Saves you from having to generate the tables at runtime, but is the most expensive option for cart space.

  • Colospace - exports the generated colorspace. Does not include any color tables, so they need to be generated at runtime or loaded separately.

  • Color Tables - exports a list of the currently generated color tables. Tables that haven't been generated yet won't be included, so you need to move the slider at the bottom of the screen to make them generate.

  • Color Table - exports only the color table for the currently selected alpha level. Color tables are stored as userdata.

The default export location is /ram/cart/

To use the data exported from this cart, you can copy the colorspace.lua file into your own cart. It contains utilities for loading and working with the colorspace data.

The usage will vary depending on if you exported the colorspace or color tables.

Working with the colorspace:

The colorspace file can be loaded with Colorspace.fetch

colorspace = Colorspace.fetch("colorspace.gfx")

If you provided a paltte for the colorspace, it has been embedded inside the colorspace file for your convenience and can be loaded with colorspace:load_palette()

You can use colorspace:get_colortable to load a color table from the colorspace object. If it doesn't exist yet it will be generated. The color space object saves a reference to the table to avoid needing to re-generate it next time it's requested.

colortable = colorspace:get_colortable(0.5)

Now that you have the color table, you need to actually apply it into memory.

colortable_index=0
colorspace:apply_colortable(colortable,colortable_index)

The second parameter here is the color table index, and can be table 0, 1, 2, or 3.

If you apply the color table to table 0, then anything your draw should appear to be blended based on that table's alpha value.
*Except if you're drawing using shapes. Shapes have their default target mask set to 0x00, but it needs to be set to 0x3f for color blending to work properly.

poke(0x550b,0x3f) --set target mask for shapes to 0x3f

There's also another utility method to get the color table and apply it in one step

colorspace:set_alpha(0.5,colortable_index)

For both apply_colortable and set_alpha, if colortable_index is omitted it will default to color table 0.

There are also functions for working with the colorspace directly without color tables

colorspace:get_color takes a red green and blue argument (numbers from 0 to 255) and returns the color index that most closely matches that color. Because it reads it off the colorspace graphic, it's actually rather quick.

c=colorspace:get_color(r,g,b)

colorspace:mix_colors takes two color indexes and an alpha value, mixes them together based on the alpha, and returns the nearest color to the result.

alpha=0.5
c3=colorspace:mix_colors(c1,c2,alpha)

Working with Color Tables

If you exported your color tables individually, you can get them with fetch

colortable = fetch("colortable.pod")

If you exported a list of color tables, they are indexed based on the alpha value.

colortables = fetch("colortables.pod")
colortable = colortables[0.5]

To apply your color table, there is a function at the top of colorspace.lua you can use.

table_index=0
apply_colortable(colortable,table_index)

As before, table_index is 0 if omitted.

That's it! Now your color table is loaded and anything you draw will be alpha blended.
*Unless you're using shapes in which case you need to make sure the target mask is set to 0x3f

Drawing with the other color tables

If you don't want write to color table zero, there are a few more steps to draw with color blending.
Let's say you applied your color table to color table 2.

apply_colortable(colortable,2)

I have two more utility functions which will let you draw using data from the other color tables.

shape_colortable sets the color table to be used when drawing shapes. it also sets the appropriate masks to allow color blending to work with shapes. Call it without arguments to set everything back to normal.

shape_colortable(2)

spr_colortable sets the color table to be used when drawing sprites. This one can only use color tables 0 or 2. It uses fillp internally with a solid fill pattern.

spr_colortable(2)

Here are the loose utility functions if you don't want to have to pull them out of colorspace.lua

function apply_colortable(data,table_number)
    table_number = table_number or 0
    local address = 0x8000+4096*table_number
    memmap(address,data:copy())
end

function shape_colortable(table_index)
    if table_index==nil then
        poke(0x5508,0x3f)--read mask
        poke(0x550b,0x00)--target mask for shapes
    else
        poke(0x5508,0xff)--read mask (need high bits)
        poke(0x550b,0x3f)--target mask for shapes
        color(table_index<<6|peek(0x550c))
    end
end

function spr_colortable(table_index)
    if table_index and table_index>1 then
        fillp(0xffff)
    else
        fillp()
    end
end
P#144322 2024-03-24 22:23 ( Edited 2024-03-25 01:30)

This is really helpful!
I think I'm now gonna try to extend the default palette to have more shades so this effect is smoother/predictable.

P#145434 2024-04-02 03:29

[Please log in to post a comment]