Skip to content

Commit

Permalink
Visualize view's visibility by text box color alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Feb 8, 2023
1 parent 227411e commit 57dd2a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RoboCanvas(width: Int, height: Int) {

fun drawText(textPointX: Float, textPointY: Float, texts: List<String>, paint: Paint) {
val graphics2D = bufferedImage.createGraphics()
graphics2D.color = Color(paint.getColor())
graphics2D.color = Color(paint.getColor(), true)

val frc: FontRenderContext = graphics2D.getFontRenderContext()
var nextY = textPointY
Expand Down Expand Up @@ -151,4 +151,11 @@ class RoboCanvas(width: Int, height: Int) {
bufferedImage.flush()
croppedImage.flush()
}

companion object {
const val TRANSPARENT_NONE = 0xFF shl 56
const val TRANSPARENT_BIT = 0xEE shl 56
const val TRANSPARENT_MEDIUM = 0x88 shl 56
const val TRANSPARENT_STRONG = 0x66 shl 56
}
}
24 changes: 13 additions & 11 deletions roborazzi/src/main/java/com/github/takahirom/roborazzi/Roborazzi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,15 @@ internal fun capture(rootComponent: RoboComponent, saveAction: (RoboCanvas) -> U
rect.right + paddingRect.left + depth * depthSlide,
rect.bottom + paddingRect.top + depth * depthSlide
)

val boxColor = colors[depth % colors.size] + (0xfF shl 56)
val alphaBoxColor = when (component.visibility) {
Visibility.Visible -> colors[depth % colors.size] + (0xEE shl 56) // alpha EE / FF
Visibility.Gone -> colors[depth % colors.size] + (0x66 shl 56) // alpha 88 / FF
Visibility.Invisible -> colors[depth % colors.size] + (0x88 shl 56) // alpha BB / FF
val boxColor = colors[depth % colors.size]
val boxAlpha = when (component.visibility) {
Visibility.Visible -> RoboCanvas.TRANSPARENT_BIT // alpha EE / FF
Visibility.Gone -> RoboCanvas.TRANSPARENT_MEDIUM // alpha 88 / FF
Visibility.Invisible -> RoboCanvas.TRANSPARENT_STRONG // alpha BB / FF
}

val alphaBoxColor = boxColor + boxAlpha

canvas.drawRect(canvasRect, paint.apply {
color = alphaBoxColor
})
Expand Down Expand Up @@ -478,17 +479,17 @@ internal fun capture(rootComponent: RoboComponent, saveAction: (RoboCanvas) -> U
)
canvas.drawRect(
textBoxRect, paint.apply {
color = boxColor
color = alphaBoxColor
})
canvas.drawText(
textPointX.toFloat() + textPadding,
textPointY.toFloat() + textPadding + rawBoxHeight / texts.size,
texts,
textPaint.apply {
color = if (isColorBright(boxColor)) {
Color.BLACK
color = if (isColorBright(alphaBoxColor)) {
Color.BLACK - RoboCanvas.TRANSPARENT_NONE + boxAlpha
} else {
Color.WHITE
Color.WHITE - RoboCanvas.TRANSPARENT_NONE + boxAlpha
}
}
)
Expand All @@ -506,10 +507,11 @@ internal fun capture(rootComponent: RoboComponent, saveAction: (RoboCanvas) -> U
}

fun isColorBright(color: Int): Boolean {
val alpha = Color.alpha(color)
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
val luminance = (0.299 * red + 0.587 * green + 0.114 * blue) / 255
val luminance = ((0.299 * red + 0.587 * green + 0.114 * blue) / 255) * alpha / 255
return luminance > 0.5
}

Expand Down

0 comments on commit 57dd2a6

Please sign in to comment.