Codetown ::: a software developer's community
Welcome to Kotlin Thursdays! Last week, we were able to render an image with TornadoFX and even manipulate its pixels. Today, we will go over Pixel Math!
Think of these resources as supplemental if you happen to be more curious. We always encourage looking into documentation for things you use!
Last week, we got the hang of how to grab these pixels and do something with them. Today, we're going to expand by creating our own filters using operational pixel manipulation.
For all practical purposes, we're going to be talking about monochromatic images. If we try to write filters using colored pixels, it will prove a lot more difficult to work with RGB values as opposed to just black or white.
Best we learn to walk before we start trying to fly!
In order to create our own image filters, we need to have a solid understanding of pixel math, or binary operations.
Binary operations are the bread and butter of computers! You can compute operations on binary values 1 and 0.
AND - both inputs must be true for the output to be true
0 && 0 = 0
0 && 1 = 0
1 && 0 = 0
1 && 1 = 1
OR - one or both inputs must be true for the output to be true
0 || 0 = 0
0 || 1 = 1
1 || 0 = 1
1 || 1 = 1
NOT - inverse result
!0 = 1
!1 = 0
!(0 && 0) = 1
!(1 || 1) = 0
Likewise, if we assign the color BLACK to 1 and the color WHITE to 0, we can easily apply binary operations to to the binary values black and white. Working with colors gets significantly more difficult when there are RGB values to consider. There are other binary operations like XANDS, XORS, and XNORS, but for now, let's just focus on the first three.
Now that we understand how OR, AND, and NOT works, let's implement these functions with colors.
fun or (a: Color, b: Color) {
return if (a == Color.BLACK || b == Color.BLACK) {
Color.BLACK
}
else { Color.WHITE
}
fun and (a: Color, b: Color) {
return if (a == Color.BLACK && b == Color.BLACK) {
Color.BLACK
} else {
Color.WHITE
}
}
fun not (color: Color) {
return if (color == Color.BLACK) Color.WHITE else Color.BLACK
}
You'll notice that these functions are for pixel colors only. Next week, we look into higher-order functions in Kotlin to learn how we can pass functions as a parameter - but you'll welcome to check out the video to see how we can apply one of these primitive filters to our images! See you next week :)
Tags:
Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.
Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.
Check out the Codetown Jobs group.
The latest feature drop for Android Studio Meerkat brings a range of updates aimed at boosting developer productivity, including extended Gemini integration for crash analysis and unit testing, enhanced Jetpack Compose previews, better Kotlin Multiplatform (KMP) integration, and more.
By Sergio De SimoneHugging Face has acquired Pollen Robotics, a French startup that developed the humanoid robot Reachy 2. The acquisition aims to make robotics more accessible by open-sourcing the robot’s design and allowing developers to modify and improve its code.
By Daniel DominguezAt the recent Google Cloud Next 2025, the cloud provider announced Rapid Storage, a new Cloud Storage zonal bucket designed to deliver consistent single-digit millisecond data access for frequently accessed data and latency-sensitive applications. The new storage class provides under 1ms random read and write latency, 20x faster data access, and 6 TB/s of throughput.
By Renato LosioThe JBang team has introduced JBang Jash, a Java library designed to simplify the execution of external processes and shell commands through a fluent and predictable API, aiming to addresses the common complexities and boilerplate developers face when using standard Java APIs like java.lang.ProcessBuilder and Runtime.exec for these tasks.
By Diogo CarletoSoftware development is shifting fast. Senior engineers need real-world insights on AI, platforms, and developer autonomy. InfoQ Dev Summit Boston (June 9-10) offers 2 days with over 27 sessions of curated, technical talks delivered by engineers actively working at scale. We are focused on helping teams navigate the software evolution, with the clarity and context needed to make better decisions.
By Eder Ignatowicz
© 2025 Created by Michael Levin.
Powered by