Skip to content

Commit

Permalink
Add .grayscale modifier support
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed Aug 28, 2024
1 parent 30733b6 commit e9c394e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,10 @@ Support levels:
<td>✅</td>
<td><code>.gradient</code> (<a href="https://skip.tools/docs/components/gradient/">example</a>)</td>
</tr>
<tr>
<td>✅</td>
<td><code>.grayscale</code></td>
</tr>
<tr>
<td>✅</td>
<td><code>.hidden</code></td>
Expand Down
37 changes: 37 additions & 0 deletions Sources/SkipUI/SkipUI/Compose/ComposeModifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 Skip
//
// This is free software: you can redistribute and/or modify it
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.ui.draw.DrawModifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas

class GrayscaleModifier : DrawModifier {
let amount: Double

init(amount: Double) {
self.amount = amount
}

// SKIP DECLARE: override fun ContentDrawScope.draw()
override func draw() {
let saturationMatrix = ColorMatrix().apply { setToSaturation(Float(max(0.0, 1.0 - amount))) }
let saturationFilter = ColorFilter.colorMatrix(saturationMatrix)
let paint = Paint().apply {
colorFilter = saturationFilter
}
drawIntoCanvas {
$0.saveLayer(Rect(Float(0.0), Float(0.0), size.width, size.height), paint)
drawContent()
$0.restore()
}
}
}
#endif
8 changes: 7 additions & 1 deletion Sources/SkipUI/SkipUI/View/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,15 @@ extension View {
#endif
}

@available(*, unavailable)
public func grayscale(_ amount: Double) -> some View {
#if SKIP
return ComposeModifierView(targetView: self) {
$0.modifier = $0.modifier.then(GrayscaleModifier(amount: amount))
return ComposeResult.ok
}
#else
return self
#endif
}

@available(*, unavailable)
Expand Down

0 comments on commit e9c394e

Please sign in to comment.