Win32 API 日本語リファレンス
ホームGraphics.Gdi › GetNearestPaletteIndex

GetNearestPaletteIndex

関数
指定色に最も近いパレットインデックスを取得する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// GDI32.dll
#include <windows.h>

DWORD GetNearestPaletteIndex(
    HPALETTE h,
    COLORREF color
);

パラメーター

名前方向説明
hHPALETTEin論理パレットへのハンドル。
colorCOLORREFin一致させる色。COLORREF 色値を作成するには、RGB マクロを使用します。

戻り値の型: DWORD

公式ドキュメント

GetNearestPaletteIndex 関数は、指定された論理パレット内で、指定された色の値に最も近いエントリのインデックスを取得します。

戻り値

関数が成功すると、戻り値は論理パレット内のエントリのインデックスになります。

関数が失敗すると、戻り値は CLR_INVALID になります。

解説(Remarks)

アプリケーションは、GetDeviceCaps 関数を呼び出し、RASTERCAPS 定数を指定することで、デバイスがパレット操作をサポートしているかどうかを判定できます。

指定した論理パレットに PC_EXPLICIT フラグが設定されたエントリが含まれている場合、戻り値は未定義です。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// GDI32.dll
#include <windows.h>

DWORD GetNearestPaletteIndex(
    HPALETTE h,
    COLORREF color
);
[DllImport("GDI32.dll", ExactSpelling = true)]
static extern uint GetNearestPaletteIndex(
    IntPtr h,   // HPALETTE
    uint color   // COLORREF
);
<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function GetNearestPaletteIndex(
    h As IntPtr,   ' HPALETTE
    color As UInteger   ' COLORREF
) As UInteger
End Function
' h : HPALETTE
' color : COLORREF
Declare PtrSafe Function GetNearestPaletteIndex Lib "gdi32" ( _
    ByVal h As LongPtr, _
    ByVal color As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetNearestPaletteIndex = ctypes.windll.gdi32.GetNearestPaletteIndex
GetNearestPaletteIndex.restype = wintypes.DWORD
GetNearestPaletteIndex.argtypes = [
    wintypes.HANDLE,  # h : HPALETTE
    wintypes.DWORD,  # color : COLORREF
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
GetNearestPaletteIndex = Fiddle::Function.new(
  lib['GetNearestPaletteIndex'],
  [
    Fiddle::TYPE_VOIDP,  # h : HPALETTE
    -Fiddle::TYPE_INT,  # color : COLORREF
  ],
  -Fiddle::TYPE_INT)
#[link(name = "gdi32")]
extern "system" {
    fn GetNearestPaletteIndex(
        h: *mut core::ffi::c_void,  // HPALETTE
        color: u32  // COLORREF
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll")]
public static extern uint GetNearestPaletteIndex(IntPtr h, uint color);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_GetNearestPaletteIndex' -Namespace Win32 -PassThru
# $api::GetNearestPaletteIndex(h, color)
#uselib "GDI32.dll"
#func global GetNearestPaletteIndex "GetNearestPaletteIndex" sptr, sptr
; GetNearestPaletteIndex h, color   ; 戻り値は stat
; h : HPALETTE -> "sptr"
; color : COLORREF -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "GDI32.dll"
#cfunc global GetNearestPaletteIndex "GetNearestPaletteIndex" sptr, int
; res = GetNearestPaletteIndex(h, color)
; h : HPALETTE -> "sptr"
; color : COLORREF -> "int"
; DWORD GetNearestPaletteIndex(HPALETTE h, COLORREF color)
#uselib "GDI32.dll"
#cfunc global GetNearestPaletteIndex "GetNearestPaletteIndex" intptr, int
; res = GetNearestPaletteIndex(h, color)
; h : HPALETTE -> "intptr"
; color : COLORREF -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procGetNearestPaletteIndex = gdi32.NewProc("GetNearestPaletteIndex")
)

// h (HPALETTE), color (COLORREF)
r1, _, err := procGetNearestPaletteIndex.Call(
	uintptr(h),
	uintptr(color),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetNearestPaletteIndex(
  h: THandle;   // HPALETTE
  color: DWORD   // COLORREF
): DWORD; stdcall;
  external 'GDI32.dll' name 'GetNearestPaletteIndex';
result := DllCall("GDI32\GetNearestPaletteIndex"
    , "Ptr", h   ; HPALETTE
    , "UInt", color   ; COLORREF
    , "UInt")   ; return: DWORD
●GetNearestPaletteIndex(h, color) = DLL("GDI32.dll", "dword GetNearestPaletteIndex(void*, dword)")
# 呼び出し: GetNearestPaletteIndex(h, color)
# h : HPALETTE -> "void*"
# color : COLORREF -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "gdi32" fn GetNearestPaletteIndex(
    h: ?*anyopaque, // HPALETTE
    color: u32 // COLORREF
) callconv(std.os.windows.WINAPI) u32;
proc GetNearestPaletteIndex(
    h: pointer,  # HPALETTE
    color: uint32  # COLORREF
): uint32 {.importc: "GetNearestPaletteIndex", stdcall, dynlib: "GDI32.dll".}
pragma(lib, "gdi32");
extern(Windows)
uint GetNearestPaletteIndex(
    void* h,   // HPALETTE
    uint color   // COLORREF
);
ccall((:GetNearestPaletteIndex, "GDI32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt32),
      h, color)
# h : HPALETTE -> Ptr{Cvoid}
# color : COLORREF -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetNearestPaletteIndex(
    void* h,
    uint32_t color);
]]
local gdi32 = ffi.load("gdi32")
-- gdi32.GetNearestPaletteIndex(h, color)
-- h : HPALETTE
-- color : COLORREF
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('GDI32.dll');
const GetNearestPaletteIndex = lib.func('__stdcall', 'GetNearestPaletteIndex', 'uint32_t', ['void *', 'uint32_t']);
// GetNearestPaletteIndex(h, color)
// h : HPALETTE -> 'void *'
// color : COLORREF -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("GDI32.dll", {
  GetNearestPaletteIndex: { parameters: ["pointer", "u32"], result: "u32" },
});
// lib.symbols.GetNearestPaletteIndex(h, color)
// h : HPALETTE -> "pointer"
// color : COLORREF -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetNearestPaletteIndex(
    void* h,
    uint32_t color);
C, "GDI32.dll");
// $ffi->GetNearestPaletteIndex(h, color);
// h : HPALETTE
// color : COLORREF
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Gdi32 extends StdCallLibrary {
    Gdi32 INSTANCE = Native.load("gdi32", Gdi32.class);
    int GetNearestPaletteIndex(
        Pointer h,   // HPALETTE
        int color   // COLORREF
    );
}
@[Link("gdi32")]
lib LibGDI32
  fun GetNearestPaletteIndex = GetNearestPaletteIndex(
    h : Void*,   # HPALETTE
    color : UInt32   # COLORREF
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetNearestPaletteIndexNative = Uint32 Function(Pointer<Void>, Uint32);
typedef GetNearestPaletteIndexDart = int Function(Pointer<Void>, int);
final GetNearestPaletteIndex = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<GetNearestPaletteIndexNative, GetNearestPaletteIndexDart>('GetNearestPaletteIndex');
// h : HPALETTE -> Pointer<Void>
// color : COLORREF -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetNearestPaletteIndex(
  h: THandle;   // HPALETTE
  color: DWORD   // COLORREF
): DWORD; stdcall;
  external 'GDI32.dll' name 'GetNearestPaletteIndex';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetNearestPaletteIndex"
  c_GetNearestPaletteIndex :: Ptr () -> Word32 -> IO Word32
-- h : HPALETTE -> Ptr ()
-- color : COLORREF -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getnearestpaletteindex =
  foreign "GetNearestPaletteIndex"
    ((ptr void) @-> uint32_t @-> returning uint32_t)
(* h : HPALETTE -> (ptr void) *)
(* color : COLORREF -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("GetNearestPaletteIndex" get-nearest-palette-index :convention :stdcall) :uint32
  (h :pointer)   ; HPALETTE
  (color :uint32))   ; COLORREF
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetNearestPaletteIndex = Win32::API::More->new('GDI32',
    'DWORD GetNearestPaletteIndex(HANDLE h, DWORD color)');
# my $ret = $GetNearestPaletteIndex->Call($h, $color);
# h : HPALETTE -> HANDLE
# color : COLORREF -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目