0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

■ PowerShellでディスプレイチェック ~あなたの液晶綺麗に使ってますか?~

Posted at

PowerShell_banner.png


こんにちは :star2:
PowerShellで投稿を濁している気がする 零壱(ゼロイチ)テクトです。

先日風邪をひいてしまって引き摺っているのと
OpenWrtTailscaleの既存の設定でごちゃごちゃやっていて
NW関連の投稿ネタがまだまとまってません。

そこで先日購入した中古のノートPC Let's note SV8
WEBでの液晶の輝度・カラーのチェックをしていたのですが
テスト画面を見ていて、ふとPowerShellでもいけるやん、
って思い、他の方も書いてないようなのでコードを書きました。

1.概要/仕様

・ディスプレイチェックに使う
・チェック画面は11種
・僕がチェックするにあたり最低限欲しいチェック画面を採用
 白全面 → 黒全面 → 赤全面 → 黄全面 → 青全面 →
 左黒~右白 → 左白~右黒 → 上黒~下白 → 上白~下黒 →
 カラーバー → 市松チェック
・Enterで次へ、ESCで終了

2.コード

DisplayChecker.ps1
Add-Type -AssemblyName System.Windows.Forms      # Windowsフォーム機能
Add-Type -AssemblyName System.Drawing            # 画像描画機能

$form = New-Object Windows.Forms.Form
$form.FormBorderStyle = 'None'                   # 枠なしウィンドウ
$form.WindowState = 'Maximized'                  # 全画面表示
$form.TopMost = $true                            # 常に最前面
$form.KeyPreview = $true                         # キーイベント取得
$form.BackgroundImageLayout = 'Stretch'          # 背景画像を伸縮表示

$global:stage = 0                                # 表示パターン番号:グローバル変数

# スクリーンパターン表示
function Show-Screen {
    # 背景初期化
    $form.BackgroundImage = $null

    switch ($stage) {
        0 { $form.BackColor = [Drawing.Color]::White }   # パターン:白
        1 { $form.BackColor = [Drawing.Color]::Black }   # パターン:黒
        2 { $form.BackColor = [Drawing.Color]::Red }     # パターン:赤
        3 { $form.BackColor = [Drawing.Color]::Yellow }  # パターン:黄
        4 { $form.BackColor = [Drawing.Color]::Blue }    # パターン:青

        5 { # グラデ:左→右 黒→白
            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height
            for ($x = 0; $x -lt $bmp.Width; $x++) {

                # 黒~白/明度計算
                $gray = [int](255 * ($x / $bmp.Width))

                # グレー表示:Y方向
                for ($y = 0; $y -lt $bmp.Height; $y++) {
                    $color = [Drawing.Color]::FromArgb($gray, $gray, $gray)  
                    $bmp.SetPixel($x, $y, $color)
                }
            }
            $form.BackgroundImage = $bmp
        }

        6 { # グラデ:左→右 白→黒
            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height
            for ($x = 0; $x -lt $bmp.Width; $x++) {

                # 白~黒/明度反転計算
                $gray = 255 - [int](255 * ($x / $bmp.Width))

                # グレー表示:Y方向
                for ($y = 0; $y -lt $bmp.Height; $y++) {
                    $color = [Drawing.Color]::FromArgb($gray, $gray, $gray)
                    $bmp.SetPixel($x, $y, $color)
                }
            }
            $form.BackgroundImage = $bmp
        }

        7 { # グラデ:上→下 黒→白
            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height
            for ($y = 0; $y -lt $bmp.Height; $y++) {

                # 黒~白/明度計算
                $gray = [int](255 * ($y / $bmp.Height))
                
                # グレー表示:X方向
                for ($x = 0; $x -lt $bmp.Width; $x++) {
                    $color = [Drawing.Color]::FromArgb($gray, $gray, $gray)
                    $bmp.SetPixel($x, $y, $color)
                }
            }
            $form.BackgroundImage = $bmp
        }

        8 { # グラデ:上→下 白→黒
            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height
            for ($y = 0; $y -lt $bmp.Height; $y++) {

                # 白~黒/明度反転計算
                $gray = 255 - [int](255 * ($y / $bmp.Height))

                # グレー表示:X方向
                for ($x = 0; $x -lt $bmp.Width; $x++) {
                    $color = [Drawing.Color]::FromArgb($gray, $gray, $gray)
                    $bmp.SetPixel($x, $y, $color)
                }
            }
            $form.BackgroundImage = $bmp
        }

        9 { # カラーバー
            $colors = @(
                [Drawing.Color]::White,
                [Drawing.Color]::Yellow,
                [Drawing.Color]::Cyan,
                [Drawing.Color]::Green,
                [Drawing.Color]::Magenta,
                [Drawing.Color]::Red,
                [Drawing.Color]::Blue
            )

            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height

            # 1色分のバー幅計算
            $barWidth = [Math]::Ceiling($bmp.Width / $colors.Count) 

            # 色数ループ
            for ($i = 0; $i -lt $colors.Count; $i++) {
                # 開始X座標
                $startX = $i * $barWidth
                
                # 終了X座標                        
                $endX = [Math]::Min($startX + $barWidth, $bmp.Width) 

                # 各色のバー表示
                for ($x = $startX; $x -lt $endX; $x++) {
                    for ($y = 0; $y -lt $bmp.Height; $y++) {
                        $bmp.SetPixel($x, $y, $colors[$i])        
                    }
                }
            }
            $form.BackgroundImage = $bmp
        }

        10 { # 市松模様
            $bmp = New-Object Drawing.Bitmap $form.Width, $form.Height

            # 市松のサイズ定義
            $size = 50
            for ($x = 0; $x -lt $bmp.Width; $x++) {
                for ($y = 0; $y -lt $bmp.Height; $y++) {

                    # XとYをブロック単位で計算
                    $checkX = [math]::Floor($x / $size) % 2
                    $checkY = [math]::Floor($y / $size) % 2

                    # X/Yが双方が同時に偶数/奇数だったら白それ以外黒。
                    # 結果、市松パターンになる。
                    if ($checkX -eq $checkY) {
                        $bmp.SetPixel($x, $y, [Drawing.Color]::White)
                    } else {
                        $bmp.SetPixel($x, $y, [Drawing.Color]::Black)
                    }
                }
            }
            $form.BackgroundImage = $bmp
        }
    }
}

# キー操作イベント:Enter=次へ、ESC=終了
$form.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") {
        $global:stage++

        if ($stage -ge 11) {
            $form.Close() 
        } else {
            Show-Screen
        }
    } elseif ($_.KeyCode -eq "Escape") {
        $form.Close()
    }
})

# 初期画面表示
Show-Screen
[void]$form.ShowDialog()

もはや教科書のサンプルレベルのしょぼしょぼコードです。
お好きにお使い、アレンジして、そして踏み越えて行って下さい。

3.結果

DisplayChecker_sample.png

それぞれ画面の映った時のスクショです。

4.備考

✅ 改善案1:カラーバーのバリエーションを増やす

✅ 改善案2:現在のチェック番号など文字表示

✅ 改善案3:モアレなどのチェックパターンを増やす

・チェック項目も探してみると意外とパターンや内容も多く
 なかなかに奥の深い世界と思った次第。

・グラデーションやカラーバー、市松については
 処理の通り1pxずつ処理しているので表示まで1~2秒かかります。

・なので待ちきれずEnterを連打するとStageが加算されて
 押した回数分スキップしちゃいます。
 対処しようと思ったのですが付け焼刃ではうまくいかず
 コードが長くなりそうだったので割愛。

・PowrShellネタは何個かあるのですが
 このディスプレイチェック同様
 内容がショボショボなので投稿のお茶濁しになるカーモ。


  それでは
       どこかの誰かの何かの足しになれば幸いです。
                        01000010 01011001 01000101

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?