Menu
Coddy logo textTech

Subtracting Objects

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 27 of 70.

Just like __add handles the + operator, the __sub metamethod defines behavior for the - operator. The pattern is identical—you define a function that receives two operands and returns a new object.

local Color = {}
Color.__index = Color

function Color:new(r, g, b)
    local obj = {r = r, g = g, b = b}
    setmetatable(obj, Color)
    return obj
end

function Color.__sub(c1, c2)
    return Color:new(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b)
end

When working with RGB colors, subtraction can produce negative values. In real applications, you might want to clamp results to valid ranges (0-255), but the core metamethod structure remains the same.

local bright = Color:new(200, 150, 100)
local dim = Color:new(50, 30, 20)
local result = bright - dim

print(result.r, result.g, result.b)  -- Output: 150  120  80

Remember: define __sub with a dot (not colon) since it's a metamethod that Lua calls directly with both operands as arguments.

challenge icon

Challenge

Easy

Let's build a Color class that supports subtraction using the - operator. Colors are represented by RGB values, and subtracting one color from another creates a new color with the difference of each component.

You'll organize your code across two files:

  • Color.lua: Define your Color class with r, g, and b components representing red, green, and blue values. Include a :new(r, g, b) constructor and implement the __sub metamethod so that subtracting two colors creates a new color with the component-wise differences. Remember that __sub uses dot syntax and receives both operands as arguments.
  • main.lua: Require your Color module, create color instances from the provided inputs, subtract them, and display the resulting RGB values.

You will receive six inputs:

  1. First color's red component
  2. First color's green component
  3. First color's blue component
  4. Second color's red component
  5. Second color's green component
  6. Second color's blue component

In your main file, create two colors with the given RGB values, subtract the second color from the first using the - operator, and print the resulting color's r, g, and b values on separate lines.

For example, if the inputs are 200, 150, 100, 50, 30, 20, the output should be:

150
120
80

The first color (200, 150, 100) minus the second color (50, 30, 20) equals a new color (150, 120, 80).

Cheat sheet

The __sub metamethod defines behavior for the - operator. It receives two operands and returns a new object.

function Color.__sub(c1, c2)
    return Color:new(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b)
end

Define __sub with a dot (not colon) since it's a metamethod that Lua calls directly with both operands as arguments.

local bright = Color:new(200, 150, 100)
local dim = Color:new(50, 30, 20)
local result = bright - dim

print(result.r, result.g, result.b)  -- Output: 150  120  80

Try it yourself

-- Require the Color module
local Color = require('Color')

-- Read input values
local r1 = tonumber(io.read())
local g1 = tonumber(io.read())
local b1 = tonumber(io.read())
local r2 = tonumber(io.read())
local g2 = tonumber(io.read())
local b2 = tonumber(io.read())

-- TODO: Create two Color instances with the given RGB values

-- TODO: Subtract the second color from the first using the - operator

-- TODO: Print the resulting color's r, g, and b values on separate lines
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming